Match-id-723e7234800b3add7829cbddeacbc4e996ba02a1

This commit is contained in:
* 2022-03-01 15:51:12 +08:00 committed by *
parent 84a05cf1fa
commit 25fc359310
5 changed files with 17 additions and 16 deletions

View File

@ -130,7 +130,7 @@ module.exports = {
//setupFiles: [], //setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test // A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: [require.resolve('./scripts/__tests__/jest/setupEnvironment.js')], setupFilesAfterEnv: [require.resolve('./scripts/__tests__/jest/deployConfiguration.js')],
// A list of paths to snapshot serializer modules Jest should use for snapshot testing // A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [], // snapshotSerializers: [],

View File

@ -17,11 +17,11 @@ describe('useState Hook Test', () => {
unmountComponentAtNode(container); unmountComponentAtNode(container);
container.remove(); container.remove();
container = null; container = null;
LogUtils.reset(); LogUtils.clear();
}); });
const Text = (props) => { const Text = (props) => {
LogUtils.injectValue(props.text); LogUtils.log(props.text);
return <p>{props.text}</p>; return <p>{props.text}</p>;
} }
@ -89,7 +89,7 @@ describe('useState Hook Test', () => {
it('useState的惰性初始化', () => { it('useState的惰性初始化', () => {
const App = forwardRef((props, ref) => { const App = forwardRef((props, ref) => {
const [num, setNum] = useState(() => { const [num, setNum] = useState(() => {
LogUtils.injectValue(props.initNum); LogUtils.log(props.initNum);
return props.initNum return props.initNum
}); });
useImperativeHandle(ref, () => ({ setNum })) useImperativeHandle(ref, () => ({ setNum }))

View File

@ -12,7 +12,7 @@ function runAssertion(fn) {
function toMatchValue(LogUtils, expectedValues) { function toMatchValue(LogUtils, expectedValues) {
return runAssertion(() => { return runAssertion(() => {
const actualValues = LogUtils.getAndClearValue(); const actualValues = LogUtils.getAndClear();
expect(actualValues).toEqual(expectedValues); expect(actualValues).toEqual(expectedValues);
}); });
} }

View File

@ -6,6 +6,7 @@ global.MessageChannel = function MessageChannel() {
}; };
}; };
// 使Jest感知自定义匹配器
expect.extend({ expect.extend({
...require('./customMatcher'), ...require('./customMatcher'),
}); });

View File

@ -1,7 +1,7 @@
let dataArray = null; let dataArray = null;
let isWorking = false; let isWorking = false;
const injectValue = (value) => { const log = (value) => {
if (dataArray === null) { if (dataArray === null) {
dataArray = [value]; dataArray = [value];
} else { } else {
@ -9,7 +9,7 @@ const injectValue = (value) => {
} }
}; };
const getAndClearValue = () => { const getAndClear = () => {
if (dataArray === null) { if (dataArray === null) {
return []; return [];
} }
@ -18,13 +18,13 @@ const getAndClearValue = () => {
return values; return values;
}; };
const reset = () => { const clear = () => {
if (isWorking) { if (isWorking) {
throw new Error('Cannot reset. There is a working task.'); throw new Error('Cannot reset. There is a working task.');
} }
dataArray = null; dataArray = null;
} }
exports.reset = reset; exports.clear = clear;
exports.injectValue = injectValue; exports.log = log;
exports.getAndClearValue = getAndClearValue; exports.getAndClear = getAndClear;