Transpile Modules with Babel in Jest Tests

Jest automatically loads and applies our babel configuration. However, because our project takes advantage of tree shaking with webpack, our babel configuration disables transpiling modules. For Jest to work on our code, we need to make sure that our babel configuration transpiles modules during our tests. We’ll use the NODE_ENV environment variable to generate the proper configuration.

For tree shaking with Webpack, need to disable transpile modules, for jest testing, we need to use commonjs

const isTest = String(process.env.NODE_ENV) === 'test'

module.exports = {
presets: [['@babel/preset-env', {modules: isTest ? 'commonjs' : false}], '@babel/preset-react'],
...
}

Configure Jest’s test environment for testing node or browser code

In our application we’re testing code that should run in the browser, but Jest is intended to test JavaScript that runs in the browser or in node. Let’s create a custom jest config file so we can customize the test environment.

If jest in your app is mean for Node application, then config jest environment to 'node', otherwise, it is mean for broswer, then 'jsdom'

Create 'jest.config.js':

module.exports = {
testEnvironment: 'jest-environment-jsdom', //'jest-environment-node',
}

If you use 'node' env, then in the test, you cannot access window object.

Support importing CSS files with Jest’s moduleNameMapper

In most real-world applications using webpack, you’re likely using a few loaders. In this lesson we’ll see how to make Jest load our modules properly with a moduleNameMapper so we can test our code that makes use of these loaders.

If you use loader in webpack to css files or graphql files which jest don't understand, you can use this trick to make it work.

Install:

npm i -D react-testing-library

In jest.config.js file:

module.exports = {
testEnvironment: 'jest-environment-jsdom', //'jest-environment-node',
moduleNameMapper: {
'\\.css$': require.resolve('./test/style-mock.js')
}
}

test/style-mock.js:

module.exports = {}

test file:

import 'react-testing-library/cleanup-after-each'
import React from 'react'
import {render} from 'react-testing-library'
import AutoScalingText from '../auto-scaling-text' test('renders', () => {
render(<AutoScalingText />)
})

component file:

...
import styles from './auto-scaling-text.module.css' class AutoScalingText extends React.Component { ... return (
<div
className={styles.autoScalingText}
...

Support using webpack CSS modules with Jest

If you’re using CSS modules with webpack, then we can improve our moduleNameMapper to improve our experience and capabilities testing our components by including the css module property name in our tests using identity-obj-proxy.

If we log out the html inside our test:

import 'react-testing-library/cleanup-after-each'
import React from 'react'
import {render} from 'react-testing-library'
import AutoScalingText from '../auto-scaling-text' test('renders', () => {
const {container} = render(<AutoScalingText />)
console.log(container.innerHTML)
})

We can see:

    console.log src/shared/__tests__/auto-scaling-text.js:
<div style="transform: scale(1,1);"></div>

There is no information about the component class, which should be:

<div className={styles.autoScalingText}

Install:

npm i -D identity-obj-proxy

in jest.config.js:

module.exports = {
testEnvironment: 'jest-environment-jsdom', //'jest-environment-node',
moduleNameMapper: {
'\\.module\\.css$': 'identity-obj-proxy',
'\\.css$': require.resolve('./test/style-mock.js')
}
}

Run:

jest

We can see:

    console.log src/shared/__tests__/auto-scaling-text.js:
<div class="autoScalingText" style="transform: scale(1,1);"></div>

Which include the class name can be a better debug / testing experience.

Generate a Serializable Value with Jest Snapshots

Snapshot testing is a way to simplify writing and maintaining assertions. As noted in the Jest documentation: “The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process. Jest uses pretty-format to make snapshots human-readable during code review. On subsequent test runs Jest will simply compare the rendered output with the previous snapshot. If they match, the test will pass. If they don't match, either the test runner found a bug in your code that should be fixed, or the implementation has changed and the snapshot needs to be updated.” Let’s see how these work and in what situations we can and should use them.

it is really easy to use snapshot in jest, just remember to update the snaphost when it is necessary by: 'npm t -- --u'

Javascript:

const superHeros = [
{name: 'Dynaguy', powers: ['disintegration ray', 'fly']},
{name: 'Apogee', powers: ['gravity control', 'fly']},
{name: 'Blazestone', powers: ['control of fire', 'pyrotechnic discharges']},
{name: 'Froozone', powers: ['freeze water']},
{name: 'Mr. Incredible', powers: ['physical strength']},
{name: 'Elastigirl', powers: ['physical stretch']},
{name: 'Violet', powers: ['invisibility', 'force fields']},
{name: 'Dash', powers: ['speed']},
{name: 'Jack-Jack', powers: ['shapeshifting', 'fly']}
] export function getFlyingSuperHeros() {
return superHeros.filter(hero => {
return hero.powers.includes('fly')
})
}
import {getFlyingSuperHeros} from '../super-heros';

test('returns super heros that can fly', () => {
const flyingHeros = getFlyingSuperHeros()
expect(flyingHeros).toMatchSnapshot();
})

snapshot:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`returns super heros that can fly 1`] = `
Array [
Object {
"name": "Dynaguy",
"powers": Array [
"disintegration ray",
"fly",
],
},
Object {
"name": "Apogee",
"powers": Array [
"gravity control",
"fly",
],
},
Object {
"name": "Jack-Jack",
"powers": Array [
"shapeshifting",
"fly",
],
},
]
`;

React Component:

import React from 'react'
import PropTypes from 'prop-types'
import AutoScalingText from './auto-scaling-text'
import {getFormattedValue} from './utils' class CalculatorDisplay extends React.Component {
static propTypes = {
value: PropTypes.string.isRequired,
}
render() {
const {value, ...props} = this.props
const formattedValue = getFormattedValue(
value,
typeof window === 'undefined' ? 'en-US' : window.navigator.language,
) return (
<div
{...props}
css={{
color: 'white',
background: '#1c191c',
lineHeight: '130px',
fontSize: '6em',
flex: '1',
}}
>
<AutoScalingText>{formattedValue}</AutoScalingText>
</div>
)
}
} export default CalculatorDisplay
import 'react-testing-library/cleanup-after-each'
import React from 'react'
import {render} from 'react-testing-library'
import CalculatorDisplay from '../calculator-display' test('mounts', () => {
const {container} = render(<CalculatorDisplay value="0" />)
expect(container.firstChild).toMatchSnapshot()
})

Snapshot:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mounts 1`] = `
<div
class="css-12149uf"
>
<div
class="autoScalingText"
style="transform: scale(1,1);"
>
0
</div>
</div>
`;

Test an Emotion Styled UI with Custom Jest Snapshot Serializers

Part of the power of snapshots is the ability to provide custom serializers. Let’s check out how to use jest-emotion to include our emotion CSS styles in our React component snapshots so we can be made aware of the impact of our CSS changes on our components.

If we look at the implementation of our component, we're getting that className from the css prop, because we're using emotion's babel plugin that will take the css prop and turn it into a className that's generated with a hash.

Snapshot:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mounts 1`] = `
<div
class="css-12149uf"
>
<div
class="autoScalingText"
style="transform: scale(1,1);"
>
0
</div>
</div>
`;

Using serializer to solve the problem:

Install:

npm install --save-dev jest-emotion
import {createSerializer} from 'jest-emotion';
import * as emotion from 'emotion'; expect.addSnapshotSerializer(createSerializer(emotion));

Snapshot:

exports[`mounts 1`] = `
.emotion-0 {
color: white;
background: #1c191c;
line-height: 130px;
font-size: 6em;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
} <div
class="emotion-0"
>

Now it shows the class and styles, it is much clear to see what has been changed.

It is also possible to add global serializer:

jest.config.js:

module.exports = {
testEnvironment: 'jest-environment-jsdom', //'jest-environment-node',
moduleNameMapper: {
'\\.module\\.css$': 'identity-obj-proxy',
'\\.css$': require.resolve('./test/style-mock.js')
},
snapshotSerializers: ['jest-serializer-path']
}

Handle Dynamic Imports using Babel with Jest

If your application is of any significant size, you will want to make use of dynamic imports to for code splitting with webpack. However, dynamic imports are not supported in node. Let’s see how this problem manifests itself and update our babel config to use babel-plugin-dynamic-import-node to simulate this in our test environment.

dynamic import:

import loadable from 'react-loadable'

const CalculatorDisplay = loadable({
loader: () => import('calculator-display').then(mod => mod.default),
loading: () => <div style={{height: 120}}>Loading display...</div>,
})

Install:

npm i -D babel-plugin-dynamic-import-node

Config babel:

plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
[
'babel-plugin-emotion',
{
hoist: isProd,
sourceMap: !isProd,
autoLabel: !isProd,
labelFormat: '[filename]--[local]',
},
],
'react-loadable/babel',
isTest ? 'babel-plugin-dynamic-import-node': null
].filter(Boolean),

[Testing] Config jest to test Javascript Application -- Part 1的更多相关文章

  1. [Testing] Config jest to test Javascript Application -- Part 2

    Setup an afterEach Test Hook for all tests with Jest setupTestFrameworkScriptFile With our current t ...

  2. [Testing] Config jest to test Javascript Application -- Part 3

    Run Jest Watch Mode by default locally with is-ci-cli In CI, we don’t want to start the tests in wat ...

  3. Web.config Transformation Syntax for Web Application Project Deployment

    Web.config Transformation Syntax for Web Application Project Deployment Other Versions   Updated: Ma ...

  4. JavaScript Application Architecture On The Road To 2015

    JavaScript Application Architecture On The Road To 2015 I once told someone I was an architect. It’s ...

  5. 转:Transform Web.Config when Deploying a Web Application Project

    Introduction One of the really cool features that are integrated with Visual Studio 2010 is Web.Conf ...

  6. spring cloud config的bootstrap.yml与application.proterties的区别

    bootstrap.yml  和application.yml  都可以用来配置参数 bootstrap.yml可以理解成系统级别的一些参数配置,这些参数一般是不会变动的 application.ym ...

  7. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  8. JavaScript Web Application summary

    Widget/ HTML DOM (CORE) (local dom) DOM, BOM, Event(Framework, UI, Widget) function(closure) DATA (c ...

  9. JavaScript Libraries In A TypeScript Application, Revisited

    If you haven’t already gotten involved with it, you’ll probably know that TypeScript is becoming inc ...

随机推荐

  1. 力扣题目汇总(重复N次元素,反转字符串,斐波那契数)

    重复 N 次的元素 1.题目描述 在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次. 返回重复了 N 次的那个元素. 示例 1: 输入:[1,2,3,3] 输出:3 ...

  2. 数据结构( Pyhon 语言描述 ) — — 第6章:继承和抽象类

    继承 新的类通过继承可以获得已有类的所有特性和行为 继承允许两个类(子类和超类)之间共享数据和方法 可以复用已有的代码,从而消除冗余性 使得软件系统的维护和验证变得简单 子类通过修改自己的方法或者添加 ...

  3. web开发框架之DRF

    RESTful架构解释: # Representational State Transfer 表现层状态转化 到底什么是RESTFul架构: 如果一个架构符合REST原则,就称它为RESTful架构. ...

  4. HUB、Switch、Router在OSI模型层次信息

    序 (HUB)集线器工作在局域网(LAN)环境,像网卡一样,应用于OSI参考模型第一层,因此又被称为物理层设备. Switch交换机工作在OSI第2层数据链路层 Router路由器工作在OSI第3层网 ...

  5. 菜鸟的《Linux程序设计》学习——MySQL数据库安装、配置及基本操作

    1. MySQL数据库: 在涉及到一些大型的Web系统或者嵌入式软件的开发时,都少不了用数据库来管理数据.在Windows操作系统下,使用过各种各样的数据库,如:sqlServer.Oracle.My ...

  6. poj 1502

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12087   Accepted: 7464 De ...

  7. Ubuntu 14.04在虚拟机上的桥接模式下设置静态IP

    1.虚拟机--->虚拟机设置 将虚拟机设置为桥接模式 2.查看window 网卡以及IP信息 cmd下输入 ipconfig -all 可以看到,我的网卡为Realtek PCIe GBE Fa ...

  8. HDU 4089 && UVa 1498 Activation 带环的概率DP

    要在HDU上交的话,要用滚动数组优化一下空间. 这道题想了很久,也算是想明白了,就好好写一下吧. P1:激活游戏失败,再次尝试. P2:连接失服务器败,从队首排到队尾. P3:激活游戏成功,队首的人出 ...

  9. 【总集】C++ STL类库 vector 使用方法

    介绍: 1.vector 的中文名为向量,可以理解为一个序列容器,里面存放的是相同的数据结构类型,类似于数组但与数组又有微妙的不同. 2.vector 采用的是连续动态的空间来存储数据,它是动态的数组 ...

  10. hdu2051

    二进制转换 #include <stdio.h> void change(int n){ ]; ; while(n){ num[cnt]=n%; n/=; cnt++; } cnt--; ...