diff --git a/jest.config.js b/jest.config.js index e9a54092..3d280082 100644 --- a/jest.config.js +++ b/jest.config.js @@ -130,7 +130,7 @@ module.exports = { //setupFiles: [], // 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 // snapshotSerializers: [], diff --git a/scripts/__tests__/ComponentTest/UseState.test.js b/scripts/__tests__/ComponentTest/UseState.test.js index aec70a42..998221bd 100644 --- a/scripts/__tests__/ComponentTest/UseState.test.js +++ b/scripts/__tests__/ComponentTest/UseState.test.js @@ -17,11 +17,11 @@ describe('useState Hook Test', () => { unmountComponentAtNode(container); container.remove(); container = null; - LogUtils.reset(); + LogUtils.clear(); }); const Text = (props) => { - LogUtils.injectValue(props.text); + LogUtils.log(props.text); return
{props.text}
; } @@ -89,7 +89,7 @@ describe('useState Hook Test', () => { it('useState的惰性初始化', () => { const App = forwardRef((props, ref) => { const [num, setNum] = useState(() => { - LogUtils.injectValue(props.initNum); + LogUtils.log(props.initNum); return props.initNum }); useImperativeHandle(ref, () => ({ setNum })) diff --git a/scripts/__tests__/jest/customMatcher.js b/scripts/__tests__/jest/customMatcher.js index a2fe5143..4fa382b3 100644 --- a/scripts/__tests__/jest/customMatcher.js +++ b/scripts/__tests__/jest/customMatcher.js @@ -12,7 +12,7 @@ function runAssertion(fn) { function toMatchValue(LogUtils, expectedValues) { return runAssertion(() => { - const actualValues = LogUtils.getAndClearValue(); + const actualValues = LogUtils.getAndClear(); expect(actualValues).toEqual(expectedValues); }); } diff --git a/scripts/__tests__/jest/setupEnvironment.js b/scripts/__tests__/jest/deployConfiguration.js similarity index 50% rename from scripts/__tests__/jest/setupEnvironment.js rename to scripts/__tests__/jest/deployConfiguration.js index 1d914211..010b9922 100644 --- a/scripts/__tests__/jest/setupEnvironment.js +++ b/scripts/__tests__/jest/deployConfiguration.js @@ -1,11 +1,12 @@ global.isDev = process.env.NODE_ENV === 'development'; global.MessageChannel = function MessageChannel() { - this.port1 = {}; - this.port2 = { - postMessage() { } - }; + this.port1 = {}; + this.port2 = { + postMessage() { } + }; }; +// 使Jest感知自定义匹配器 expect.extend({ - ...require('./customMatcher'), + ...require('./customMatcher'), }); diff --git a/scripts/__tests__/jest/logUtils.js b/scripts/__tests__/jest/logUtils.js index 4182b0ce..42d1248f 100644 --- a/scripts/__tests__/jest/logUtils.js +++ b/scripts/__tests__/jest/logUtils.js @@ -1,7 +1,7 @@ let dataArray = null; let isWorking = false; -const injectValue = (value) => { +const log = (value) => { if (dataArray === null) { dataArray = [value]; } else { @@ -9,7 +9,7 @@ const injectValue = (value) => { } }; -const getAndClearValue = () => { +const getAndClear = () => { if (dataArray === null) { return []; } @@ -18,13 +18,13 @@ const getAndClearValue = () => { return values; }; -const reset = () => { +const clear = () => { if (isWorking) { throw new Error('Cannot reset. There is a working task.'); } dataArray = null; } -exports.reset = reset; -exports.injectValue = injectValue; -exports.getAndClearValue = getAndClearValue; +exports.clear = clear; +exports.log = log; +exports.getAndClear = getAndClear;