/
build.test.js
80 lines (71 loc) · 2.67 KB
/
build.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import execa from 'execa';
import { buildStorybook, setSourceDir, setSpawnParams } from './build';
jest.mock('execa');
describe('setSourceDir', () => {
it('sets a random temp directory path on the context', async () => {
const ctx = { options: {}, storybook: { version: '5.0.0' } };
await setSourceDir(ctx);
expect(ctx.sourceDir).toMatch(/chromatic-/);
});
it('falls back to the default output dir for older Storybooks', async () => {
const ctx = { options: {}, storybook: { version: '4.0.0' } };
await setSourceDir(ctx);
expect(ctx.sourceDir).toBe('storybook-static');
});
it('uses the outputDir option if provided', async () => {
const ctx = { options: { outputDir: 'storybook-out' }, storybook: { version: '5.0.0' } };
await setSourceDir(ctx);
expect(ctx.sourceDir).toBe('storybook-out');
});
it('uses the outputDir option if provided, even for older Storybooks', async () => {
const ctx = { options: { outputDir: 'storybook-out' }, storybook: { version: '4.0.0' } };
await setSourceDir(ctx);
expect(ctx.sourceDir).toBe('storybook-out');
});
});
describe('setSpawnParams', () => {
it('sets the spawn params on the context', async () => {
process.env.npm_execpath = 'npm';
const ctx = { sourceDir: './source-dir/', options: { buildScriptName: 'build:storybook' } };
await setSpawnParams(ctx);
expect(ctx.spawnParams).toEqual({
command: 'npm',
clientArgs: ['run', '--silent'],
scriptArgs: ['build:storybook', '--', '--output-dir', './source-dir/'],
});
});
});
describe('buildStorybook', () => {
it('runs the build command', async () => {
const ctx = {
spawnParams: {
command: 'build:storybook',
clientArgs: ['--client-args'],
scriptArgs: ['--script-args'],
},
env: { STORYBOOK_BUILD_TIMEOUT: 1000 },
};
await buildStorybook(ctx);
expect(ctx.buildLogFile).toMatch(/build-storybook\.log$/);
expect(execa).toHaveBeenCalledWith(
'build:storybook',
['--client-args', '--script-args'],
expect.objectContaining({ stdio: expect.any(Array) })
);
});
it('fails when build times out', async () => {
const ctx = {
spawnParams: {
command: 'build:storybook',
clientArgs: ['--client-args'],
scriptArgs: ['--script-args'],
},
options: { buildScriptName: '' },
env: { STORYBOOK_BUILD_TIMEOUT: 0 },
log: { error: jest.fn() },
};
execa.mockReturnValue(new Promise((resolve) => setTimeout(resolve, 10)));
await expect(buildStorybook(ctx)).rejects.toThrow('Command failed');
expect(ctx.log.error).toHaveBeenCalledWith(expect.stringContaining('Operation timed out'));
});
});