Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decorateSnapshotUi: get file from stacktrace #88950

Merged
merged 5 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
"base64-js": "^1.3.1",
"base64url": "^3.0.1",
"broadcast-channel": "^3.0.3",
"callsites": "^3.1.0",
"chai": "3.5.0",
"chance": "1.0.18",
"chromedriver": "^87.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,32 @@ import { decorateSnapshotUi, expectSnapshot } from './decorate_snapshot_ui';
import path from 'path';
import fs from 'fs';

const createMockTest = ({
title = 'Test',
passed = true,
}: { title?: string; passed?: boolean } = {}) => {
return {
fullTitle: () => title,
isPassed: () => passed,
parent: {},
} as Test;
};

describe('decorateSnapshotUi', () => {
const snapshotFolder = path.resolve(__dirname, '__snapshots__');
const snapshotFile = path.resolve(snapshotFolder, 'decorate_snapshot_ui.test.snap');

const cleanup = () => {
if (fs.existsSync(snapshotFile)) {
fs.unlinkSync(snapshotFile);
fs.rmdirSync(snapshotFolder);
}
};

beforeEach(cleanup);

afterAll(cleanup);

describe('when running a test', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
Expand All @@ -21,15 +46,7 @@ describe('decorateSnapshotUi', () => {
});

it('passes when the snapshot matches the actual value', async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;
const test = createMockTest();

await lifecycle.beforeEachTest.trigger(test);

Expand All @@ -39,15 +56,7 @@ describe('decorateSnapshotUi', () => {
});

it('throws when the snapshot does not match the actual value', async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;
const test = createMockTest();

await lifecycle.beforeEachTest.trigger(test);

Expand All @@ -57,27 +66,10 @@ describe('decorateSnapshotUi', () => {
});

it('writes a snapshot to an external file if it does not exist', async () => {
const test: Test = {
title: 'Test',
file: __filename,
isPassed: () => true,
} as any;

// @ts-expect-error
test.parent = {
file: __filename,
tests: [test],
suites: [],
};
const test: Test = createMockTest();

await lifecycle.beforeEachTest.trigger(test);

const snapshotFile = path.resolve(
__dirname,
'__snapshots__',
'decorate_snapshot_ui.test.snap'
);

expect(fs.existsSync(snapshotFile)).toBe(false);

expect(() => {
Expand All @@ -87,10 +79,48 @@ describe('decorateSnapshotUi', () => {
await lifecycle.afterTestSuite.trigger(test.parent);

expect(fs.existsSync(snapshotFile)).toBe(true);
});
});

fs.unlinkSync(snapshotFile);
describe('when writing multiple snapshots to a single file', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
lifecycle = new Lifecycle();
decorateSnapshotUi({ lifecycle, updateSnapshots: false, isCi: false });
});

beforeEach(() => {
fs.mkdirSync(path.resolve(__dirname, '__snapshots__'));
fs.writeFileSync(
snapshotFile,
`// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[\`Test1 1\`] = \`"foo"\`;

exports[\`Test2 1\`] = \`"bar"\`;
`,
{ encoding: 'utf-8' }
);
});

it('compares to an existing snapshot', async () => {
const test1 = createMockTest({ title: 'Test1' });

await lifecycle.beforeEachTest.trigger(test1);

expect(() => {
expectSnapshot('foo').toMatch();
}).not.toThrow();

const test2 = createMockTest({ title: 'Test2' });

fs.rmdirSync(path.resolve(__dirname, '__snapshots__'));
await lifecycle.beforeEachTest.trigger(test2);

expect(() => {
expectSnapshot('foo').toMatch();
}).toThrow();

await lifecycle.afterTestSuite.trigger(test1.parent);
});
});

Expand All @@ -102,15 +132,7 @@ describe('decorateSnapshotUi', () => {
});

it("doesn't throw if the value does not match", async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;
const test = createMockTest();

await lifecycle.beforeEachTest.trigger(test);

Expand All @@ -128,21 +150,90 @@ describe('decorateSnapshotUi', () => {
});

it('throws on new snapshots', async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;
const test = createMockTest();

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('bar').toMatchInline();
}).toThrow();
});

describe('when adding to an existing file', () => {
beforeEach(() => {
fs.mkdirSync(path.resolve(__dirname, '__snapshots__'));
fs.writeFileSync(
snapshotFile,
`// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[\`Test 1\`] = \`"foo"\`;

exports[\`Test2 1\`] = \`"bar"\`;
`,
{ encoding: 'utf-8' }
);
});

it('does not throw on an existing test', async () => {
const test = createMockTest({ title: 'Test' });

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('foo').toMatch();
}).not.toThrow();
});

it('throws on a new test', async () => {
const test = createMockTest({ title: 'New test' });

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('foo').toMatch();
}).toThrow();
});

it('does not throw when all snapshots are used ', async () => {
const test = createMockTest({ title: 'Test' });

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('foo').toMatch();
}).not.toThrow();

const test2 = createMockTest({ title: 'Test2' });

await lifecycle.beforeEachTest.trigger(test2);

expect(() => {
expectSnapshot('bar').toMatch();
}).not.toThrow();

const afterTestSuite = lifecycle.afterTestSuite.trigger({});

await expect(afterTestSuite).resolves.toBe(undefined);
});

it('throws on unused snapshots', async () => {
const test = createMockTest({ title: 'Test' });

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('foo').toMatch();
}).not.toThrow();

const afterTestSuite = lifecycle.afterTestSuite.trigger({});

await expect(afterTestSuite).rejects.toMatchInlineSnapshot(`
[Error: 1 obsolete snapshot(s) found:
Test2 1.

Run tests again with \`--updateSnapshots\` to remove them.]
`);
});
});
});
});
Loading