-
-
Notifications
You must be signed in to change notification settings - Fork 339
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
Add jest tests for create-toolpad-app
#1965
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
653466b
test create-toolpad-app using health-check
bharatkashyap 964c682
Merge branch 'master' into cta-test
bharatkashyap cecde2e
Propose improved tests
Janpot 0a3dae3
forgiot this
Janpot 7bc8415
Try running tests separately
Janpot 116b6dc
path fix
Janpot 304885f
Rename test, remove the old one
bharatkashyap 8f10ea3
test create-toolpad-app using health-check
bharatkashyap 1dec69a
Propose improved tests
Janpot 7dad262
forgiot this
Janpot fc07627
Try running tests separately
Janpot b469274
path fix
Janpot 79ce916
Rename test, remove the old one
bharatkashyap 8f3132e
Merge branch 'cta-test' of github.com:bharatkashyap/mui-toolpad into …
bharatkashyap a2ee8d8
Merge branch 'master' into cta-test
bharatkashyap bccad7a
Feat: Start app and run health-check
bharatkashyap e8b8eca
Fix: Use `@mui/toolpad-utils` and add `install` op
bharatkashyap f7756a9
add check
Janpot ac372c7
dynamic import ESM
Janpot 63320e4
Remove dynamic imports
Janpot 7275165
try adding a small delay
Janpot c818649
Merge branch 'master' into cta-test
bharatkashyap c4f0058
trty longer timeout
Janpot 2207b70
hello
Janpot fb914e7
max retries
Janpot 98e813e
Dispose
Janpot 8e6c599
Merge branch 'master' into cta-test
bharatkashyap 902fbf8
Don't dispose?
Janpot 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 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,5 @@ | ||
import { Config } from 'jest'; | ||
|
||
export default { | ||
preset: 'ts-jest/presets/default-esm', | ||
} satisfies Config; |
This file contains 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 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 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,98 @@ | ||
import * as fs from 'fs/promises'; | ||
import { jest } from '@jest/globals'; | ||
import * as path from 'path'; | ||
import * as url from 'url'; | ||
import { execa, ExecaChildProcess } from 'execa'; | ||
import readline from 'readline'; | ||
import { Readable } from 'stream'; | ||
import { once } from 'events'; | ||
|
||
jest.setTimeout(60000); | ||
|
||
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
const cliPath = path.resolve(currentDirectory, '../dist/index.js'); | ||
|
||
let testDir: string | undefined; | ||
let cp: ExecaChildProcess<string> | undefined; | ||
let toolpadProcess: ExecaChildProcess<string> | undefined; | ||
|
||
async function waitForMatch(input: Readable, regex: RegExp): Promise<RegExpExecArray | null> { | ||
return new Promise((resolve, reject) => { | ||
const rl = readline.createInterface({ input }); | ||
|
||
rl.on('line', (line) => { | ||
const match = regex.exec(line); | ||
if (match) { | ||
rl.close(); | ||
input.resume(); | ||
resolve(match); | ||
} | ||
}); | ||
rl.on('error', (err) => reject(err)); | ||
rl.on('end', () => resolve(null)); | ||
}); | ||
} | ||
|
||
test('create-toolpad-app can bootstrap a Toolpad app', async () => { | ||
testDir = await fs.mkdtemp(path.resolve(currentDirectory, './test-app-')); | ||
cp = execa(cliPath, [path.basename(testDir)], { | ||
cwd: currentDirectory, | ||
}); | ||
const result = await cp; | ||
expect(result.stdout).toMatch('Run the following to get started'); | ||
const packageJsonContent = await fs.readFile(path.resolve(testDir, './package.json'), { | ||
encoding: 'utf-8', | ||
}); | ||
const packageJson = JSON.parse(packageJsonContent); | ||
expect(packageJson).toEqual( | ||
expect.objectContaining({ | ||
dependencies: expect.objectContaining({ | ||
'@mui/toolpad': expect.any(String), | ||
}), | ||
scripts: expect.objectContaining({ | ||
build: 'toolpad build', | ||
dev: 'toolpad dev', | ||
start: 'toolpad start', | ||
}), | ||
}), | ||
); | ||
|
||
toolpadProcess = execa('yarn', ['dev'], { | ||
cwd: testDir, | ||
env: { | ||
FORCE_COLOR: '0', | ||
BROWSER: 'none', | ||
}, | ||
}); | ||
const { stdout: toolpadDevOutput } = toolpadProcess; | ||
|
||
expect(toolpadDevOutput).toBeTruthy(); | ||
const match = await waitForMatch(toolpadDevOutput!, /http:\/\/localhost:(\d+)/); | ||
|
||
expect(match).toBeTruthy(); | ||
|
||
const appUrl = match![0]; | ||
const res = await fetch(`${appUrl}/health-check`); | ||
expect(res).toHaveProperty('status', 200); | ||
}); | ||
|
||
afterEach(async () => { | ||
if (toolpadProcess && typeof toolpadProcess.exitCode !== 'number') { | ||
toolpadProcess.kill('SIGKILL'); | ||
await once(toolpadProcess, 'exit'); | ||
} | ||
}); | ||
|
||
afterEach(async () => { | ||
if (testDir) { | ||
await fs.rm(testDir, { recursive: true, force: true, maxRetries: 3 }); | ||
} | ||
}); | ||
|
||
afterEach(async () => { | ||
if (cp && typeof cp.exitCode !== 'number') { | ||
cp.kill('SIGKILL'); | ||
await once(cp, 'exit'); | ||
} | ||
}); |
This file contains 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,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains 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,6 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "ESNext" | ||
} | ||
} |
This file contains 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 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
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.
Weren't you going to try running the app in this test as well?
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.
Yes, I'm currently on that (also adding the
no-install
option to make running this test quicker)