-
Notifications
You must be signed in to change notification settings - Fork 85
chore: add tests to runtime #75
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
39d69b2
chore: add tests to runtime
Nemikolh 16a07de
fix: make sure @tutorialkit/runtime is picked up by CI as well
Nemikolh 2f036e0
fix: code review
Nemikolh bfcb319
Merge branch 'main' into joan/runtime-tests
Nemikolh b6c10d3
fix: simplify imports and add more test to TutorialRunner
Nemikolh 8661e57
fix: lint issue
Nemikolh fd42d43
Merge branch 'main' into joan/runtime-tests
SamVerschueren 2c6f75b
fix: code review and simplification
Nemikolh d9b60cc
Merge branch 'main' into joan/runtime-tests
Nemikolh 95e5b2f
Update packages/test-utils/package.json
Nemikolh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ dist-ssr | |
.pnpm-store | ||
/tutorialkit/template | ||
tsconfig.tsbuildinfo | ||
tsconfig.build.tsbuildinfo | ||
.tmp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pnpm-lock.yaml | ||
.astro | ||
**/*.md | ||
**/*.mdx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
"template:build": "pnpm run build && pnpm run --filter=tutorialkit-starter build", | ||
"docs": "pnpm run --filter=tutorialkit.dev dev", | ||
"docs:build": "pnpm run --filter=tutorialkit.dev build", | ||
"test": "pnpm run --filter=tutorialkit test" | ||
"test": "pnpm run --filter=@tutorialkit/* --filter=tutorialkit test" | ||
}, | ||
"license": "MIT", | ||
"packageManager": "[email protected]", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, test, expect, beforeAll, vi, afterAll, beforeEach } from 'vitest'; | ||
import { LessonFilesFetcher } from './lesson-files.js'; | ||
|
||
const originalFetch = global.fetch; | ||
|
||
let fetchBody: any; | ||
|
||
const fetchSpy = vi.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(fetchBody), | ||
}), | ||
); | ||
|
||
beforeAll(() => { | ||
global.fetch = fetchSpy as any; | ||
}); | ||
|
||
beforeEach(() => { | ||
fetchSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
global.fetch = originalFetch; | ||
}); | ||
|
||
describe('LessonFilesFetcher', () => { | ||
test('getLessonTemplate should fetch template', async () => { | ||
fetchBody = { 'a.txt': 'content' }; | ||
|
||
const fetcher = new LessonFilesFetcher(); | ||
const files = await fetcher.getLessonTemplate({ data: { template: 'default' } } as any); | ||
|
||
expect(files).toEqual({ 'a.txt': 'content' }); | ||
expect(fetchSpy).toHaveBeenCalledWith('/template-default.json', expect.anything()); | ||
}); | ||
|
||
test('getLessonFiles should fetch files', async () => { | ||
fetchBody = { 'a.txt': 'content' }; | ||
|
||
const fetcher = new LessonFilesFetcher(); | ||
const files = await fetcher.getLessonFiles({ | ||
files: ['1-welcome-1-intro-1-welcome-files.json', ['a.txt']], | ||
} as any); | ||
|
||
expect(files).toEqual({ 'a.txt': 'content' }); | ||
expect(fetchSpy).toHaveBeenCalledWith('/1-welcome-1-intro-1-welcome-files.json'); | ||
}); | ||
|
||
test('getLessonSolution should fetch solution', async () => { | ||
fetchBody = { 'a.txt': 'content' }; | ||
|
||
const fetcher = new LessonFilesFetcher(); | ||
const files = await fetcher.getLessonSolution({ | ||
solution: ['1-welcome-1-intro-1-welcome-solution.json', ['a.txt']], | ||
} as any); | ||
|
||
expect(files).toEqual({ 'a.txt': 'content' }); | ||
expect(fetchSpy).toHaveBeenCalledWith('/1-welcome-1-intro-1-welcome-solution.json'); | ||
}); | ||
|
||
test('invalidate should return none if files are not in map', async () => { | ||
const fetcher = new LessonFilesFetcher(); | ||
const result = await fetcher.invalidate('1-welcome-1-intro-1-welcome-files.json'); | ||
|
||
expect(result).toEqual({ type: 'none' }); | ||
}); | ||
|
||
test('invalidate should fetch files', async () => { | ||
fetchBody = { 'a.txt': 'content' }; | ||
|
||
const fetcher = new LessonFilesFetcher(); | ||
|
||
const initialData = await fetcher.getLessonFiles({ | ||
files: ['1-welcome-1-intro-1-welcome-files.json', ['a.txt']], | ||
} as any); | ||
|
||
expect(initialData).toEqual({ 'a.txt': 'content' }); | ||
|
||
fetchBody = { 'a.txt': 'foobar' }; | ||
|
||
const result = await fetcher.invalidate('1-welcome-1-intro-1-welcome-files.json'); | ||
|
||
expect(result).toEqual({ type: 'files', files: { 'a.txt': 'foobar' } }); | ||
expect(fetchSpy).toHaveBeenCalledWith('/1-welcome-1-intro-1-welcome-files.json'); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { resetProcessFactory, setProcessFactory } from 'test-utils'; | ||
import { WebContainer } from '@webcontainer/api'; | ||
import { describe, test, expect, beforeEach, vi } from 'vitest'; | ||
import type { MockedWebContainer } from 'test-utils'; | ||
import { TutorialRunner } from './tutorial-runner.js'; | ||
import { StepsController } from './webcontainer/steps.js'; | ||
import { TerminalStore } from './store/terminal.js'; | ||
import { withResolvers } from './utils/promises.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 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>(() => {}), | ||
}; | ||
}); | ||
|
||
setProcessFactory(processFactory); | ||
|
||
const runner = new TutorialRunner(webcontainer, new TerminalStore(webcontainer, false), 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, | ||
}, | ||
}, | ||
] | ||
`); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { Commands, Command } from './command.js'; | ||
|
||
describe('Commands', () => { | ||
it('should accept shell like commands', () => { | ||
const commands = new Commands({ | ||
mainCommand: 'npm run start', | ||
prepareCommands: ['npm install'], | ||
}); | ||
|
||
expect(commands.mainCommand?.shellCommand).toBe('npm run start'); | ||
expect(commands.prepareCommands?.[0].shellCommand).toBe('npm install'); | ||
}); | ||
|
||
it('should accept a tuple of [shell, title]', () => { | ||
const commands = new Commands({ | ||
mainCommand: ['npm run start', 'Start dev server'], | ||
prepareCommands: [['npm install', 'Installing dependencies']], | ||
}); | ||
|
||
expect(commands.mainCommand?.shellCommand).toBe('npm run start'); | ||
expect(commands.mainCommand?.title).toBe('Start dev server'); | ||
expect(commands.prepareCommands?.[0].shellCommand).toBe('npm install'); | ||
expect(commands.prepareCommands?.[0].title).toBe('Installing dependencies'); | ||
}); | ||
|
||
it('should accept objects with { title, command }', () => { | ||
const commands = new Commands({ | ||
mainCommand: { command: 'npm run start', title: 'Start dev server' }, | ||
prepareCommands: [{ command: 'npm install', title: 'Installing dependencies' }], | ||
}); | ||
|
||
expect(commands.mainCommand?.shellCommand).toBe('npm run start'); | ||
expect(commands.mainCommand?.title).toBe('Start dev server'); | ||
expect(commands.prepareCommands?.[0].shellCommand).toBe('npm install'); | ||
expect(commands.prepareCommands?.[0].title).toBe('Installing dependencies'); | ||
}); | ||
|
||
it('should be iterable', () => { | ||
const commands = new Commands({ | ||
mainCommand: 'npm run start', | ||
prepareCommands: ['npm install', 'npm run build'], | ||
}); | ||
|
||
const iterator: Iterator<Command> = commands[Symbol.iterator](); | ||
expect(iterator.next().value.shellCommand).toBe('npm install'); | ||
expect(iterator.next().value.shellCommand).toBe('npm run build'); | ||
expect(iterator.next().value.shellCommand).toBe('npm run start'); | ||
expect(iterator.next().done).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Command', () => { | ||
it('should be runnable if shell command is not empty', () => { | ||
const command = new Command('npm install'); | ||
expect(command.isRunnable()).toBe(true); | ||
}); | ||
|
||
it('should not be runnable if shell command is empty', () => { | ||
const command = new Command(''); | ||
expect(command.isRunnable()).toBe(false); | ||
}); | ||
|
||
it('should compare commands by shell command', () => { | ||
const a = new Command('npm install'); | ||
const b = new Command('npm install'); | ||
const c = new Command('npm run start'); | ||
|
||
expect(Command.equals(a, b)).toBe(true); | ||
expect(Command.equals(a, c)).toBe(false); | ||
}); | ||
|
||
it('should extract title from command', () => { | ||
const command = new Command({ command: 'npm install', title: 'Installing dependencies' }); | ||
expect(command.title).toBe('Installing dependencies'); | ||
}); | ||
|
||
it('should convert command to shell command', () => { | ||
const command = new Command({ command: 'npm install', title: 'Installing dependencies' }); | ||
expect(command.shellCommand).toBe('npm install'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
.astro
files be gitignored?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
.astro
folder is ignored already, I noticed that prettier was giving a warning locally because it doesn't ignore it.It doesn't produce that warning on CI because we run prettier before running the build script. If the order was reversed, the CI would fail which would be a bit silly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OOh weird. Ok, then it's fine.