Skip to content

Commit

Permalink
conf: add test.skipOnWatch for snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 23, 2020
1 parent f7ae45b commit 4839ac8
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 72 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = {
'no-useless-computed-key': 2,
'jest/expect-expect': 0,
'jest/no-identical-title': 0,
'jest/no-standalone-expect': 0,
'arrow-body-style': [2, 'as-needed'],
'object-shorthand': [2, 'always', { avoidQuotes: true }],
},
Expand Down
2 changes: 1 addition & 1 deletion config/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
clearMocks: true,
rootDir: '../../',
roots: ['<rootDir>'],
setupFiles: [path.join(CONFIG_PATH, 'jest/jest.setup.js')],
setupFilesAfterEnv: [path.join(CONFIG_PATH, 'jest/jest.setup.js')],
testEnvironment: 'node',
testMatch: ['**/*/(spec|test).js', '**/*.(spec|test).js'],
transform: {
Expand Down
10 changes: 10 additions & 0 deletions config/jest/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ global.isWatchMode = (process.argv || []).some(
);

glob.sync(packagePath('**', 'jest.setup.js')).forEach(require);

/* eslint-disable */
test.skipOnWatch = (...args) => {
if (global.isWatchMode) {
return test.skip(...args);
}

return test(...args);
};
/* eslint-enable */
23 changes: 13 additions & 10 deletions packages/vest/src/core/state/reset/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ const spec = _vest => {
createSuite(['field_1', 'field_3']);
});

test('Sanity - making sure everything works as it should', () => {
const suiteState = getSuiteState(suiteId);
expect(suiteState.lagging).toHaveLength(2);
expect(suiteState.pending).toHaveLength(2);
expect(suiteState.lagging[0].fieldName).toBe('field_1');
expect(suiteState.pending[0].fieldName).toBe('field_2');
expect(suiteState.lagging[1].fieldName).toBe('field_3');
expect(suiteState.pending[1].fieldName).toBe('field_4');
expect(getState()).toMatchSnapshot();
});
test.skipOnWatch(
'Sanity - making sure everything works as it should',
() => {
const suiteState = getSuiteState(suiteId);
expect(suiteState.lagging).toHaveLength(2);
expect(suiteState.pending).toHaveLength(2);
expect(suiteState.lagging[0].fieldName).toBe('field_1');
expect(suiteState.pending[0].fieldName).toBe('field_2');
expect(suiteState.lagging[1].fieldName).toBe('field_3');
expect(suiteState.pending[1].fieldName).toBe('field_4');
expect(getState()).toMatchSnapshot();
}
);

it('Should remove suite from state', () => {
expect(getSuites()).toHaveProperty(suiteId);
Expand Down
31 changes: 17 additions & 14 deletions packages/vest/src/spec/integration.async-tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ runSpec(vest => {
beforeEach(() => {
resetState();
});
it('Should have all fields', () =>
new Promise(done => {
// ❗️Why is this test async? Because of the `resetState` beforeEach.
// We must not clean up before the suite is actually done.
result = suite(vest).done(done);
expect(result.tests).toHaveProperty('field_1');
expect(result.tests).toHaveProperty('field_2');
expect(result.tests).toHaveProperty('field_4');
expect(result.tests).toHaveProperty('field_5');
expect(result.tests).toHaveProperty('field_6');
expect(result.tests).toHaveProperty('field_7');
expect(result.hasErrors('field_7')).toBe(false);
expect(result.tests).toMatchSnapshot();
}));
test.skipOnWatch(
'Should have all fields',
() =>
new Promise(done => {
// ❗️Why is this test async? Because of the `resetState` beforeEach.
// We must not clean up before the suite is actually done.
result = suite(vest).done(done);
expect(result.tests).toHaveProperty('field_1');
expect(result.tests).toHaveProperty('field_2');
expect(result.tests).toHaveProperty('field_4');
expect(result.tests).toHaveProperty('field_5');
expect(result.tests).toHaveProperty('field_6');
expect(result.tests).toHaveProperty('field_7');
expect(result.hasErrors('field_7')).toBe(false);
expect(result.tests).toMatchSnapshot();
})
);

it('Should invoke done callback specified with sync field immediately, and the others after finishing', () =>
new Promise(done => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/spec/integration.base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ runSpec(vest => {
res = suite(vest);
});

it('Should produce correct validation result', () => {
test.skipOnWatch('Should produce correct validation result', () => {
expect(res.tests).toHaveProperty('field_1');
expect(res.tests).toHaveProperty('field_2');
expect(res.tests).toHaveProperty('field_3');
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/spec/integration.stateful-tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ runSpec(vest => {
resetState();
});
let result;
it('Should merge skipped fields with previous values', () => {
test.skipOnWatch('Should merge skipped fields with previous values', () => {
result = suite(vest, 'field_1');
expect(result.tests.field_1.errorCount).toBe(1);
expect(result.errorCount).toBe(1);
Expand Down
93 changes: 48 additions & 45 deletions packages/vest/src/spec/integration.stateless-tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,56 @@ runSpec(vest => {
resetState();
});
let result;
it('Should start suite with no previous data on each run', () =>
new Promise(done => {
// ✅ First suite tests basic behavior, and callback registration
result = suite(vest, 'field_1')
.done(callback_1)
.done('field_1', callback_2)
.done('field_2', callback_3);
expect(result.tests.field_1.errorCount).toBe(1);
expect(result.errorCount).toBe(1);
expect(Object.keys(result.tests)).toHaveLength(1);
expect(result.tests).toHaveProperty('field_1');
expect(callback_1).toHaveBeenCalled();
expect(callback_2).toHaveBeenCalled();
expect(callback_3).not.toHaveBeenCalled();
expect(result).toMatchSnapshot();
test.skipOnWatch(
'Should start suite with no previous data on each run',
() =>
new Promise(done => {
// ✅ First suite tests basic behavior, and callback registration
result = suite(vest, 'field_1')
.done(callback_1)
.done('field_1', callback_2)
.done('field_2', callback_3);
expect(result.tests.field_1.errorCount).toBe(1);
expect(result.errorCount).toBe(1);
expect(Object.keys(result.tests)).toHaveLength(1);
expect(result.tests).toHaveProperty('field_1');
expect(callback_1).toHaveBeenCalled();
expect(callback_2).toHaveBeenCalled();
expect(callback_3).not.toHaveBeenCalled();
expect(result).toMatchSnapshot();

// ✅ Second suite to test that values do not get merged
result = suite(vest, 'field_5');
expect(result.errorCount).toBe(2);
expect(result.tests).not.toHaveProperty('field_1');
expect(result.tests.field_5.errorCount).toBe(2);
expect(Object.keys(result.tests)).toHaveLength(1);
expect(result.tests).toHaveProperty('field_5');
expect(result).toMatchSnapshot();
// ✅ Second suite to test that values do not get merged
result = suite(vest, 'field_5');
expect(result.errorCount).toBe(2);
expect(result.tests).not.toHaveProperty('field_1');
expect(result.tests.field_5.errorCount).toBe(2);
expect(Object.keys(result.tests)).toHaveLength(1);
expect(result.tests).toHaveProperty('field_5');
expect(result).toMatchSnapshot();

// ✅ Last suite tests that even without skipping
// Nothing gets merged and that we can still register the
// callbacks - even after delay
result = suite(vest);
expect(result.errorCount).toBe(5);
expect(result.tests.field_1.errorCount).toBe(1);
expect(result.tests.field_2.errorCount).toBe(1);
expect(result.tests.field_3.errorCount).toBe(1);
expect(result.tests.field_4.warnCount).toBe(1);
expect(result.tests.field_5.errorCount).toBe(2);
expect(Object.keys(result.tests)).toHaveLength(5);
expect(result).toMatchSnapshot();
setTimeout(() => {
// Testing that even though the state got discarded
// We still register consumer callbacks
expect(callback_4).not.toHaveBeenCalled();
result.done(callback_4);
expect(callback_4).toHaveBeenCalled();
isDeepCopy(callback_4.mock.calls[0][0], result);
done();
});
}));
// ✅ Last suite tests that even without skipping
// Nothing gets merged and that we can still register the
// callbacks - even after delay
result = suite(vest);
expect(result.errorCount).toBe(5);
expect(result.tests.field_1.errorCount).toBe(1);
expect(result.tests.field_2.errorCount).toBe(1);
expect(result.tests.field_3.errorCount).toBe(1);
expect(result.tests.field_4.warnCount).toBe(1);
expect(result.tests.field_5.errorCount).toBe(2);
expect(Object.keys(result.tests)).toHaveLength(5);
expect(result).toMatchSnapshot();
setTimeout(() => {
// Testing that even though the state got discarded
// We still register consumer callbacks
expect(callback_4).not.toHaveBeenCalled();
result.done(callback_4);
expect(callback_4).toHaveBeenCalled();
isDeepCopy(callback_4.mock.calls[0][0], result);
done();
});
})
);
});
});

Expand Down

0 comments on commit 4839ac8

Please sign in to comment.