Skip to content

Commit

Permalink
feat(add): jest
Browse files Browse the repository at this point in the history
  • Loading branch information
seognil committed Jan 21, 2020
1 parent e41d184 commit 8990502
Show file tree
Hide file tree
Showing 8 changed files with 4,301 additions and 0 deletions.
17 changes: 17 additions & 0 deletions testing/jest/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
plugins: [
'@babel/plugin-syntax-dynamic-import',

['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',

// ['@babel/plugin-transform-runtime', { corejs: 3 }],
],
presets: [
['@babel/preset-env', { modules: 'commonjs' }],
'@babel/preset-react',
'@babel/preset-typescript',
],
};
4 changes: 4 additions & 0 deletions testing/jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
verbose: true,
collectCoverage: true,
};
19 changes: 19 additions & 0 deletions testing/jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"scripts": {
"test": "jest",
"test:watch": "jest -w"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@babel/preset-react": "^7.8.3",
"@babel/preset-typescript": "^7.8.3",
"@types/jest": "^24.9.0",
"jest": "^24.9.0"
}
}
5 changes: 5 additions & 0 deletions testing/jest/src/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { add } from './add';

test('test add function', () => {
expect(add(1, 2)).toBe(3);
});
1 change: 1 addition & 0 deletions testing/jest/src/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const add = (a: number, b: number) => a + b;
182 changes: 182 additions & 0 deletions testing/jest/src/single.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// * ------------------------------------------------ 基本使用

describe('描述:基本使用', () => {
test('简单测试项', () => {
// expect.hasAssertions();
expect(2).toBe(2);
});
test('简单测试项2', () => {
// expect.assertions(2);
expect('hello').toEqual('hello');
expect('hello').not.toEqual('world');
});
});

describe('基本类型简单测试', () => {
test('数字', () => {
expect(0.1 + 0.2).not.toEqual(0.3);
expect(0.1 + 0.2).toBeCloseTo(0.3);

expect(1 + 2).toBeLessThanOrEqual(4);

expect(Infinity / Infinity).toBeNaN();
});
test('字符串/数组', () => {
expect('Quick fox').toMatch(/quick/i);

expect('Quick fox').toContain('ck fo');
expect(['Hello', 'world']).toContain('Hello');

expect([{ id: 1 }, { id: 2 }]).not.toContain({ id: 1 });
expect([{ id: 1 }, { id: 2 }]).toContainEqual({ id: 1 });
expect([{ id: 1 }, { id: 2 }]).not.toContainEqual({ id: 1, name: 'John' });

expect('Quick').toHaveLength(5);
expect(['Hello', 'world']).toHaveLength(2);
});
});

// * ------------------------------------------------ 假值检测

describe('Nullish', () => {
test('', () => {
const obj = {};

// @ts-ignore
expect(obj.key).toBeUndefined();
expect(undefined).toBeUndefined();

expect(undefined).not.toBeDefined();
expect(null).toBeDefined();
expect(0).toBeDefined();

expect(null).toBeNull();
expect(undefined).not.toBeNull();

expect(0).toBeFalsy();
expect('').toBeFalsy();
expect(null).toBeFalsy();

expect('0').toBeTruthy();
expect([]).toBeTruthy();
});
});

// * ------------------------------------------------ 容易混淆的相等性 API

describe('容易混淆的相等性 API,对象', () => {
test('toBe', () => {
expect({ hello: 'world' }).not.toBe({ hello: 'world' });

const obj = { hello: 'world' };
expect(obj).toBe(obj);
});

test('toEqual', () => {
expect({ hello: 'world' }).toEqual({ hello: 'world' });
expect({ hello: 'world' }).toEqual({ hello: 'world', verb: undefined });
});

test('toStrictEqual', () => {
expect({ hello: 'world' }).toStrictEqual({ hello: 'world' });
expect({ hello: 'world' }).not.toStrictEqual({ hello: 'world', verb: undefined });
});

test('toMatchObject', () => {
expect({
val: 2,
arr: [4, 3, 2, 1],
hello: 'world',
}).toMatchObject({
val: expect.any(Number),
arr: expect.arrayContaining([1, 2]),
});
});
});

// * ------------------------------------------------ Mock 系列

describe('Mock 系列', () => {
test('jest.fn', () => {
const hello = (name = 'John') => `Hello ${name}`;
const mockFn = jest.fn(hello);
mockFn.mockName('hello there');

mockFn('Chirs');

// console.log(mockFn.getMockName()); // => hello there
// console.log(mockFn.mock.calls); // => [ [ 'Chirs' ] ]
// console.log(mockFn.mock.results); // => [ { type: 'return', value: 'Hello Chirs' } ]
// console.log(mockFn.mock.instances); // => [ undefined ]

expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('Chirs');
expect(mockFn).toHaveReturnedTimes(1);
expect(mockFn).toHaveReturnedWith('Hello Chirs');
});

test('jest.spyOn', () => {
const obj = { hello: (name = 'John') => `Hello ${name}` };

const spyFn = jest.spyOn(obj, 'hello');

obj.hello('Freddie');
obj.hello('Bob');

// console.log(spyFn.getMockName()); // => jest.fn()
// console.log(spyFn.mock.calls); // => [ [ 'Freddie' ], [ 'Bob' ] ]
// console.log(spyFn.mock.results); // =>
// [
// { type: 'return', value: 'Hello Freddie' },
// { type: 'return', value: 'Hello Bob' }
// ]

// console.log(spyFn.mock.instances); // => [ { hello: [Function: mockConstructor] {} }, {...} ]

expect(spyFn).toHaveBeenCalled();
expect(spyFn).toHaveBeenCalledTimes(2);
expect(spyFn).toHaveBeenCalledWith('Freddie');
expect(spyFn).toHaveBeenLastCalledWith('Bob');
expect(spyFn).toHaveBeenNthCalledWith(1, 'Freddie');
expect(spyFn).toHaveReturned();
expect(spyFn).toHaveReturnedTimes(2);
expect(spyFn).toHaveReturnedWith('Hello Bob');
expect(spyFn).toHaveLastReturnedWith('Hello Bob');
expect(spyFn).toHaveNthReturnedWith(2, 'Hello Bob');
});

test('mock 实例方法', () => {
// * ---------------- 等价写法
{
const hello = (name = 'John') => `Hello ${name}`;
jest.fn(hello);
jest.fn().mockImplementation(hello);
}

// * ---------------- mockImplementationOnce
{
const myMockFn = jest
.fn()
.mockImplementationOnce((cb) => cb(null, true))
.mockImplementationOnce((cb) => cb(null, false));

expect([myMockFn((err, val) => val), myMockFn((err, val) => val)]).toEqual([true, false]);
}

// * ---------------- mockReturnValueOnce
{
const myMockFn = jest
.fn()
.mockReturnValue('default')
.mockReturnValueOnce('first call')
.mockReturnValueOnce('second call');

expect([myMockFn(), myMockFn(), myMockFn(), myMockFn()]).toEqual([
'first call',
'second call',
'default',
'default',
]);
}
});
});
8 changes: 8 additions & 0 deletions testing/jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"types": [
"jest",
"node"
]
}
}
Loading

0 comments on commit 8990502

Please sign in to comment.