-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtutorial-runner.spec.ts
More file actions
145 lines (128 loc) · 3.62 KB
/
tutorial-runner.spec.ts
File metadata and controls
145 lines (128 loc) · 3.62 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// must be imported first
import { resetProcessFactory, setProcessFactory } from '@tutorialkit/test-utils';
import type { MockedWebContainer } from '@tutorialkit/test-utils';
import { WebContainer } from '@webcontainer/api';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { withResolvers } from '../utils/promises.js';
import { StepsController } from '../webcontainer/steps.js';
import { EditorStore } from './editor.js';
import { TerminalStore } from './terminal.js';
import { TutorialRunner } from './tutorial-runner.js';
beforeEach(() => {
resetProcessFactory();
});
describe('TutorialRunner', () => {
test('prepareFiles should mount files to WebContainer', async () => {
const webcontainer = WebContainer.boot();
const mock = (await webcontainer) as MockedWebContainer;
const runner = new TutorialRunner(
webcontainer,
new TerminalStore(webcontainer, false),
new EditorStore(),
new StepsController(),
);
await runner.prepareFiles({
files: {
'/src/index.js': 'console.log("Hello, world!")',
'/src/index.html': '<h1>Hello, world!</h1>',
},
template: {
'/package.json': '{ "name": "my-project" }',
},
});
expect(mock._fakeFs).toMatchInlineSnapshot(`
{
"package.json": {
"file": {
"contents": "{ "name": "my-project" }",
},
},
"src": {
"directory": {
"index.html": {
"file": {
"contents": "<h1>Hello, world!</h1>",
},
},
"index.js": {
"file": {
"contents": "console.log("Hello, world!")",
},
},
},
},
}
`);
});
test('runCommands should execute commands only after load has completed', async () => {
const webcontainer = WebContainer.boot();
const mock = (await webcontainer) as MockedWebContainer;
const { promise: runCommandPromise, resolve } = withResolvers();
const processFactory = vi.fn(() => {
resolve(JSON.stringify(mock._fakeFs, undefined, 2));
return {
exit: new Promise<number>(() => {
// noop
}),
};
});
setProcessFactory(processFactory);
const runner = new TutorialRunner(
webcontainer,
new TerminalStore(webcontainer, false),
new EditorStore(),
new StepsController(),
);
runner.setCommands({
mainCommand: 'some command',
});
runner.prepareFiles({
files: {
'/src/index.js': 'console.log("Hello, world!")',
'/src/index.html': '<h1>Hello, world!</h1>',
},
template: {
'/package.json': '{ "name": "my-project" }',
},
});
runner.runCommands();
const fs = await runCommandPromise;
expect(processFactory).toHaveBeenCalledTimes(1);
expect(fs).toMatchInlineSnapshot(`
"{
"package.json": {
"file": {
"contents": "{ \\"name\\": \\"my-project\\" }"
}
},
"src": {
"directory": {
"index.js": {
"file": {
"contents": "console.log(\\"Hello, world!\\")"
}
},
"index.html": {
"file": {
"contents": "<h1>Hello, world!</h1>"
}
}
}
}
}"
`);
expect(mock._fakeProcesses).toMatchInlineSnapshot(`
[
{
"args": [
"command",
],
"command": "some",
"options": {
"terminal": undefined,
},
},
]
`);
});
});