Вы можете использовать jest.spyOn (object, methodName, accessType?) , чтобы смоделировать возвращаемое значение для каждого тестового случая.
Например,
contants.ts
:
export function get() {
return {
build: 'dist',
action: {
pusher: {
name: 'montezuma',
email: 'best@cat'
},
gitHubToken: 'exists'
}
};
}
index.ts
:
import { get } from './constants';
export async function init() {
return get();
}
index.spec.ts
:
import { init } from './';
import * as constants from './constants';
describe('git', () => {
describe('init', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('t-1', async () => {
const mConstants = {
build: '/',
action: {
pusher: {
name: 'asd',
email: 'as@cat'
},
gitHubToken: 'ccsa'
}
};
const spy = jest.spyOn(constants, 'get').mockReturnValueOnce(mConstants);
const actualValue = await init();
expect(actualValue).toEqual(mConstants);
expect(spy).toBeCalledTimes(1);
});
it('t-2', async () => {
const mConstants = {
build: 'build',
action: {
pusher: {
name: 'zzz',
email: 'www@cat'
},
gitHubToken: 'xxx'
}
};
const spy = jest.spyOn(constants, 'get').mockReturnValueOnce(mConstants);
const actualValue = await init();
expect(actualValue).toEqual(mConstants);
expect(spy).toBeCalledTimes(1);
});
});
});
Результат модульного теста:
PASS src/stackoverflow/58758771/index.spec.ts (7.685s)
git
init
✓ t-1 (5ms)
✓ t-2 (2ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 8.829s
Обновление , мы также можем изменить объект констант для каждого случая модульного теста.
Перед запуском модульного теста мы должны сохранить исходные константы. После завершения каждого теста мы должны восстановить исходные константы. Я использую метод lodash.cloneDeep
, чтобы сделать глубокий клон для констант.
constants.ts
:
export const constants = {
build: 'dist',
action: {
pusher: {
name: 'montezuma',
email: 'best@cat'
},
gitHubToken: 'exists'
}
};
index.ts
:
import { constants } from './constants';
export async function init() {
return constants;
}
index.spec.ts
:
import { init } from './';
import { constants } from './constants';
import _ from 'lodash';
const originalConstants = _.cloneDeep(constants);
describe('git', () => {
afterEach(() => {
_.assignIn(constants, originalConstants);
});
describe('init', () => {
it('t-1', async () => {
Object.assign(constants, {
build: '/',
action: {
...constants.action,
pusher: { ...constants.action.pusher, name: 'aaa', email: 'aaa@cat' },
gitHubToken: 'bbb'
}
});
const actualValue = await init();
expect(actualValue).toEqual({
build: '/',
action: {
pusher: {
name: 'aaa',
email: 'aaa@cat'
},
gitHubToken: 'bbb'
}
});
});
it('should restore original contants', () => {
expect(constants).toEqual({
build: 'dist',
action: {
pusher: {
name: 'montezuma',
email: 'best@cat'
},
gitHubToken: 'exists'
}
});
});
});
});
Результат модульного теста:
PASS src/stackoverflow/58758771/v2/index.spec.ts (10.734s)
git
init
✓ t-1 (7ms)
✓ should restore original contants (1ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 12.509s
Исходный код: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58758771