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

Add jest tests for create-toolpad-app #1965

Merged
merged 28 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 May 3, 2023
964c682
Merge branch 'master' into cta-test
bharatkashyap May 3, 2023
cecde2e
Propose improved tests
Janpot May 4, 2023
0a3dae3
forgiot this
Janpot May 4, 2023
7bc8415
Try running tests separately
Janpot May 4, 2023
116b6dc
path fix
Janpot May 4, 2023
304885f
Rename test, remove the old one
bharatkashyap May 5, 2023
8f10ea3
test create-toolpad-app using health-check
bharatkashyap May 3, 2023
1dec69a
Propose improved tests
Janpot May 4, 2023
7dad262
forgiot this
Janpot May 4, 2023
fc07627
Try running tests separately
Janpot May 4, 2023
b469274
path fix
Janpot May 4, 2023
79ce916
Rename test, remove the old one
bharatkashyap May 5, 2023
8f3132e
Merge branch 'cta-test' of github.com:bharatkashyap/mui-toolpad into …
bharatkashyap May 9, 2023
a2ee8d8
Merge branch 'master' into cta-test
bharatkashyap May 9, 2023
bccad7a
Feat: Start app and run health-check
bharatkashyap May 9, 2023
e8b8eca
Fix: Use `@mui/toolpad-utils` and add `install` op
bharatkashyap May 9, 2023
f7756a9
add check
Janpot May 9, 2023
ac372c7
dynamic import ESM
Janpot May 9, 2023
63320e4
Remove dynamic imports
Janpot May 9, 2023
7275165
try adding a small delay
Janpot May 9, 2023
c818649
Merge branch 'master' into cta-test
bharatkashyap May 10, 2023
c4f0058
trty longer timeout
Janpot May 10, 2023
2207b70
hello
Janpot May 10, 2023
fb914e7
max retries
Janpot May 10, 2023
98e813e
Dispose
Janpot May 10, 2023
8e6c599
Merge branch 'master' into cta-test
bharatkashyap May 11, 2023
902fbf8
Don't dispose?
Janpot May 11, 2023
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 jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const config: Config.InitialOptions = {
projects: [
'<rootDir>/packages/toolpad-app/jest.config.ts',
'<rootDir>/packages/toolpad-utils/jest.config.ts',
'<rootDir>/packages/create-toolpad-app/jest.config.ts',
],
setupFilesAfterEnv: ['<rootDir>/test/utils/jest-setup.ts'],
};
Expand Down
6 changes: 6 additions & 0 deletions packages/create-toolpad-app/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Config } from 'jest';

export default {
preset: 'ts-jest',
transform: {},
bharatkashyap marked this conversation as resolved.
Show resolved Hide resolved
} satisfies Config;
74 changes: 74 additions & 0 deletions packages/create-toolpad-app/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { spawn, exec } = require('child_process');
bharatkashyap marked this conversation as resolved.
Show resolved Hide resolved
const fs = require('fs');
const { rimraf } = require('rimraf');
const http = require('http');

interface ServerResponse {
statusCode: number;
}

describe('create-toolpad-app', () => {
let toolpadProcess: ReturnType<typeof exec>;
let cp: ReturnType<typeof spawn>;
let toolpadAppAddress = '';
const toolpadAppAddressRegex = /http:\/\/localhost:(\d+)/;
const directoryPath = './my-test-app';

test('create-toolpad-app launches the app', (done) => {
const scriptPath = './packages/create-toolpad-app/dist/index.js';
cp = spawn('node', [scriptPath, directoryPath]);
const { stderr } = cp;

stderr.on('data', (data: Buffer) => {
console.error(data.toString());
});

cp.on('error', (err: Error) => {
console.error(`Failed to start create-toolpad-app: ${err}`);
});

cp.on('exit', (code: number | null, signal: string | null) => {
if (code !== 0) {
console.error(`create-toolpad-app exited with code ${code}`);
} else if (signal !== null) {
console.error(`create-toolpad-app exited due to signal ${signal}`);
} else {
toolpadProcess = exec('yarn dev', {
cwd: directoryPath,
});

toolpadProcess.on('error', (err: Error) => {
console.error(`Failed to start toolpad dev: ${err}`);
bharatkashyap marked this conversation as resolved.
Show resolved Hide resolved
});

toolpadProcess.stdout.on('data', (data: Buffer) => {
// Match the URL in a line which says "ready on <URL>":
const matches = data.toString().match(toolpadAppAddressRegex);
if (matches) {
toolpadAppAddress = matches[0];
done();
}
});
}
});
}, 60000);

it('should return a 200 status code when hitting the health-check route', (done) => {
http.get(`${toolpadAppAddress}/health-check`, (res: ServerResponse) => {
expect(res.statusCode).toBe(200);
bharatkashyap marked this conversation as resolved.
Show resolved Hide resolved
done();
});
});

afterAll(() => {
if (toolpadProcess) {
toolpadProcess.kill();
}

toolpadProcess.on('exit', () => {
if (fs.existsSync(directoryPath)) {
rimraf(directoryPath);
}
});
});
});