Skip to content

Commit

Permalink
Add tests for custom server (#2798)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Oct 13, 2023
1 parent 4f8a835 commit 34b6b53
Show file tree
Hide file tree
Showing 36 changed files with 455 additions and 141 deletions.
5 changes: 5 additions & 0 deletions packages/toolpad-app/src/server/EnvManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ export default class EnvManager {
async getDeclaredValues(): Promise<Record<string, string>> {
return this.values;
}

// eslint-disable-next-line class-methods-use-this
getEnv() {
return process.env;
}
}
25 changes: 13 additions & 12 deletions packages/toolpad-app/src/server/FunctionsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { errorFrom } from '@mui/toolpad-utils/errors';
import { ToolpadDataProviderIntrospection } from '@mui/toolpad-core/runtime';
import * as url from 'node:url';
import invariant from 'invariant';
import EnvManager from './EnvManager';
import { ProjectEvents, ToolpadProjectOptions } from '../types';
import { createWorker as createDevWorker } from './functionsDevWorker';
Expand Down Expand Up @@ -113,7 +114,7 @@ export default class FunctionsManager {

private buildErrors: esbuild.Message[] = [];

private devWorker: ReturnType<typeof createDevWorker>;
private devWorker: ReturnType<typeof createDevWorker> | undefined;

private extractedTypes: Awaitable<IntrospectionResult> | undefined;

Expand All @@ -123,7 +124,6 @@ export default class FunctionsManager {

constructor(project: IToolpadProject) {
this.project = project;
this.devWorker = createDevWorker(process.env);
}

private getResourcesFolder(): string {
Expand Down Expand Up @@ -172,7 +172,7 @@ export default class FunctionsManager {
private async extractTypes() {
if (!this.extractTypesWorker) {
this.extractTypesWorker = new Piscina({
filename: path.join(currentDirectory, 'functionsTypesWorker.js'),
filename: path.resolve(currentDirectory, '../cli/functionsTypesWorker.js'),
});
}

Expand Down Expand Up @@ -252,26 +252,24 @@ export default class FunctionsManager {
resourcesWatcher.on('unlink', reinitializeWatcher);
}

private async createRuntimeWorkerWithEnv() {
private async createRuntimeWorker() {
const oldWorker = this.devWorker;
this.devWorker = createDevWorker(process.env);

await oldWorker.terminate();

this.devWorker = createDevWorker(this.project.envManager.getEnv());
await oldWorker?.terminate();
this.project.invalidateQueries();
}

async start() {
await this.createRuntimeWorker();

if (this.project.options.dev) {
await this.migrateLegacy();

await this.startWatchingFunctionFiles();

this.project.events.subscribe('envChanged', async () => {
await this.createRuntimeWorkerWithEnv();
await this.createRuntimeWorker();
});

await this.createRuntimeWorkerWithEnv();
}
}

Expand All @@ -297,7 +295,7 @@ export default class FunctionsManager {
async dispose() {
await Promise.all([
this.disposeBuildcontext(),
this.devWorker.terminate(),
this.devWorker?.terminate(),
this.extractTypesWorker?.destroy(),
]);
}
Expand Down Expand Up @@ -344,6 +342,7 @@ export default class FunctionsManager {
? [{ parameters }]
: handler.parameters.map(([parameterName]) => parameters[parameterName]);

invariant(this.devWorker, 'devWorker must be initialized');
const data = await this.devWorker.execute(outputFilePath, name, executeParams);

return { data };
Expand Down Expand Up @@ -388,6 +387,7 @@ export default class FunctionsManager {
exportName: string = 'default',
): Promise<ToolpadDataProviderIntrospection> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.introspectDataProvider(fullPath, exportName);
}

Expand All @@ -397,6 +397,7 @@ export default class FunctionsManager {
params: GetRecordsParams<R, P>,
): Promise<GetRecordsResult<R, P>> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.getDataProviderRecords(fullPath, exportName, params);
}
}
10 changes: 9 additions & 1 deletion test/integration/backend-basic/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as url from 'url';
import invariant from 'invariant';
import { expect, test } from '../../playwright/localTest';
import { ToolpadRuntime } from '../../models/ToolpadRuntime';
import { expectBasicPageContent } from './shared';
Expand All @@ -15,8 +16,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'dev',
env: {
SECRET_BAZ: 'Some baz secret',
Expand All @@ -26,6 +29,11 @@ test.use({
});

test('base path basics', async ({ page, context, localApp }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

await context.addCookies([
{ name: 'MY_TOOLPAD_COOKIE', value: 'foo-bar-baz', domain: 'localhost', path: '/' },
]);
Expand Down
10 changes: 9 additions & 1 deletion test/integration/backend-basic/baseProd.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as url from 'url';
import invariant from 'invariant';
import { expect, test } from '../../playwright/localTest';
import { ToolpadRuntime } from '../../models/ToolpadRuntime';
import { expectBasicPageContent } from './shared';
Expand All @@ -15,8 +16,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'start',
env: {
SECRET_BAZ: 'Some baz secret',
Expand All @@ -26,6 +29,11 @@ test.use({
});

test('base path basics', async ({ page, context, localApp }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

await context.addCookies([
{ name: 'MY_TOOLPAD_COOKIE', value: 'foo-bar-baz', domain: 'localhost', path: '/' },
]);
Expand Down
20 changes: 19 additions & 1 deletion test/integration/backend-basic/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as url from 'url';
import invariant from 'invariant';
import { fileReplace } from '../../../packages/toolpad-utils/src/fs';
import { test, expect } from '../../playwright/localTest';
import { ToolpadRuntime } from '../../models/ToolpadRuntime';
Expand Down Expand Up @@ -28,8 +29,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'dev',
env: {
SECRET_BAZ: 'Some baz secret',
Expand All @@ -49,6 +52,11 @@ test('functions basics', async ({ page, context }) => {
});

test('function editor reload', async ({ page, localApp }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

const editorModel = new ToolpadEditor(page);
await editorModel.goToPageById(BASIC_TESTS_PAGE_ID);

Expand All @@ -68,6 +76,11 @@ test('function editor reload', async ({ page, localApp }) => {
});

test('function editor parameters update', async ({ page, localApp, argosScreenshot }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

const editorModel = new ToolpadEditor(page);
await editorModel.goToPageById(BASIC_TESTS_PAGE_ID);

Expand Down Expand Up @@ -143,6 +156,11 @@ test('Extracted types', async ({ page }) => {
});

test('function editor extracted parameters', async ({ page, localApp }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

const editorModel = new ToolpadEditor(page);
await editorModel.goToPageById(EXTRACTED_TYPES_PAGE_ID);

Expand Down
4 changes: 3 additions & 1 deletion test/integration/backend-basic/prod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'start',
env: {
SECRET_BAZ: 'Some baz secret',
Expand Down
10 changes: 9 additions & 1 deletion test/integration/basic-auth/prod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as url from 'url';
import invariant from 'invariant';
import { test, expect } from '../../playwright/localTest';

const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));
Expand All @@ -11,8 +12,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'start',
env: {
TOOLPAD_BASIC_AUTH_USER: 'foo',
Expand All @@ -32,6 +35,11 @@ test('Access is blocked to API route', async ({ request }) => {
});

test('Access is granted when authenticated', async ({ browserName, page, localApp }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

test.skip(
browserName === 'firefox',
'Fails due to https://bugzilla.mozilla.org/show_bug.cgi?id=1742396',
Expand Down
4 changes: 3 additions & 1 deletion test/integration/bindings/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand Down
4 changes: 3 additions & 1 deletion test/integration/bindings/navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { expect, test } from '../../playwright/localTest';
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture-navigation'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand Down
10 changes: 9 additions & 1 deletion test/integration/codeComponents/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as path from 'path';
import * as fs from 'fs/promises';
import * as url from 'url';
import invariant from 'invariant';
import { ToolpadRuntime } from '../../models/ToolpadRuntime';
import { expect, test } from '../../playwright/localTest';
import { ToolpadEditor } from '../../models/ToolpadEditor';

const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand All @@ -23,6 +26,11 @@ test('custom components can use external libraries', async ({ page }) => {
});

test('can create new custom components', async ({ page, localApp }) => {
invariant(
localApp,
'test must be configured with `localAppConfig`. Add `test.use({ localAppConfig: ... })`',
);

const editorModel = new ToolpadEditor(page);

await editorModel.goto();
Expand Down
4 changes: 3 additions & 1 deletion test/integration/components/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ async function waitForComponents(page: Page, frame: Page | FrameLocator = page)
}

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture-basic'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand Down
4 changes: 3 additions & 1 deletion test/integration/components/chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ test.use({
});

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture-chart'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand Down
4 changes: 3 additions & 1 deletion test/integration/components/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { test, expect } from '../../playwright/localTest';
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture-form'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand Down
4 changes: 3 additions & 1 deletion test/integration/components/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { test, expect } from '../../playwright/localTest';
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));

test.use({
localAppConfig: {
projectConfig: {
template: path.resolve(currentDirectory, './fixture-list'),
},
localAppConfig: {
cmd: 'dev',
},
});
Expand Down
Loading

0 comments on commit 34b6b53

Please sign in to comment.