Skip to content

Commit b995491

Browse files
committed
Use root config as a shared configuration for vitest instead
1 parent 53c3210 commit b995491

File tree

9 files changed

+62
-163
lines changed

9 files changed

+62
-163
lines changed

devserver/src/components/__tests__/Playground.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Playground tests', () => {
2828
test('Running js-slang by clicking the run button', async () => {
2929
const component = render(<Playground />);
3030
await clickRunButton(component);
31-
expect(runInContext).toHaveBeenCalled();
31+
await expect.poll(() => runInContext).toHaveBeenCalled();
3232
});
3333

3434
test('Loading tabs via Vite', async () => {
@@ -37,7 +37,7 @@ describe('Playground tests', () => {
3737
const component = render(<Playground />);
3838

3939
await clickRunButton(component);
40-
expect(runInContext).toHaveBeenCalled();
40+
await expect.poll(() => runInContext).toHaveBeenCalled();
4141
expect(importers.getDynamicTabs).toHaveBeenCalled();
4242
});
4343

@@ -54,26 +54,7 @@ describe('Playground tests', () => {
5454
await userEvent.click(settingsButton);
5555

5656
await clickRunButton(component);
57-
expect(runInContext).toHaveBeenCalled();
57+
await expect.poll(() => runInContext).toHaveBeenCalled();
5858
expect(importers.getCompiledTabs).toHaveBeenCalled();
5959
});
60-
61-
test('Multiple evaluations', async () => {
62-
const component = render(<Playground />);
63-
64-
await commands.setLocalStorage('editorValue', 'delete this');
65-
await clickRunButton(component);
66-
67-
const crossIcon = component.baseElement.getElementsByClassName('bp5-icon bp5-icon-cross').item(0);
68-
console.log(crossIcon);
69-
await userEvent.click(crossIcon!);
70-
71-
const replCard = component.baseElement.getElementsByTagName('pre');
72-
console.log(replCard);
73-
74-
await commands.setLocalStorage('editorValue', '0;');
75-
await clickRunButton(component);
76-
77-
expect(runInContext).toHaveBeenCalledTimes(2);
78-
});
7960
});

lib/buildtools/build.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
*/
44

55
// @ts-check
6+
import pathlib from 'path';
67
import getBuildCommand from '@sourceacademy/lib-compiler';
78

9+
const repoRoot = pathlib.resolve(import.meta.dirname, '../..');
10+
811
const command = getBuildCommand({
912
entryPoints: ['./src/commands/index.ts'],
1013
banner: {
@@ -17,6 +20,24 @@ const command = getBuildCommand({
1720
platform: 'node',
1821
target: 'node20',
1922
tsconfig: './tsconfig.json',
23+
plugins: [{
24+
name: 'Local Externalizer',
25+
setup({ onResolve }) {
26+
onResolve({
27+
filter: /^\.{1,2}(\/.+)?/
28+
}, args => {
29+
const absolutePath = pathlib.resolve(args.resolveDir, args.path);
30+
const { dir, ext, base } = pathlib.parse(absolutePath);
31+
if (ext === '.json' || dir !== repoRoot) return undefined;
32+
33+
const newDirectory = pathlib.relative('./bin', repoRoot);
34+
return {
35+
external: true,
36+
path: pathlib.join(newDirectory, base),
37+
};
38+
});
39+
}
40+
}]
2041
});
2142

2243
await command.parseAsync();

lib/buildtools/src/testing/configs.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1-
import pathlib from 'path';
21
import react from '@vitejs/plugin-react';
3-
import { coverageConfigDefaults, defineConfig, defineProject, mergeConfig } from 'vitest/config';
4-
import { getGitRoot } from '../getGitRoot.js';
2+
import { defineProject, mergeConfig, type ViteUserConfig } from 'vitest/config';
3+
// @ts-expect-error I'm too lazy to make the root config work with typescript
4+
import rootConfig from '../../../../vitest.config.js';
55

6-
const gitRoot = await getGitRoot();
6+
rootConfig.test.projects = undefined;
7+
rootConfig.test.environment = 'jsdom';
78

89
/**
910
* A shared Vitest configuration object that can be combined with {@link mergeConfig}
1011
* to create custom Vitest configurations for each sub project
1112
*/
12-
export const sharedVitestConfiguration = defineConfig({
13-
resolve: {
14-
alias: [{
15-
find: /^js-slang\/context/,
16-
replacement: pathlib.join(gitRoot, 'src/__mocks__/context.ts')
17-
}]
18-
},
19-
test: {
20-
clearMocks: true,
21-
coverage: {
22-
provider: 'v8',
23-
exclude: [
24-
...coverageConfigDefaults.exclude,
25-
'./build/**',
26-
'**/__mocks__/**',
27-
'**/__test_mocks__/**',
28-
'**/bin/**',
29-
'**/build.js',
30-
'**/*.config.?(c)[jt]s',
31-
'**/dist.?(c)js',
32-
'./docs',
33-
'**/src/**/samples/**',
34-
'./lib/buildtools/src/build/docs/drawdown.ts'
35-
]
36-
},
37-
silent: 'passed-only',
38-
}
39-
});
13+
export const sharedVitestConfiguration: ViteUserConfig = rootConfig;
4014

4115
/**
4216
* Vitest configuration specific to tabs

lib/buildtools/src/testing/runner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export async function runVitest(mode: VitestRunMode, filters: string[] | undefin
2323
},
2424
}
2525
);
26+
2627
const vitest = await startVitest(mode, filters, finalConfig);
2728

2829
if (vitest.shouldKeepServer()) {

lib/buildtools/src/testing/utils.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ async function getBundleOrTabTestConfiguration(asset: ResolvedTab | ResolvedBund
125125
}
126126

127127
setBrowserOptions(indivConfig, asset, watch);
128-
129128
return indivConfig;
130129
}
131130

@@ -135,14 +134,11 @@ async function getBundleOrTabTestConfiguration(asset: ResolvedTab | ResolvedBund
135134
indivConfig = cloneDeep(sharedVitestConfiguration);
136135
}
137136

138-
if (!indivConfig.test) {
139-
throw new Error(`Vitest config for ${asset.type} does not contain a 'test' section!`);
140-
}
141-
142-
indivConfig.test.name = `${capitalize(asset.name)} ${capitalize(asset.type)}`;
143-
indivConfig.test.root = asset.directory;
137+
indivConfig!.test!.name = `${capitalize(asset.name)} ${capitalize(asset.type)}`;
138+
indivConfig!.test!.root = asset.directory;
139+
indivConfig!.test!.include = ['**/__tests__/**/*.{ts,tsx}'];
144140

145-
setBrowserOptions(indivConfig, asset, watch);
141+
setBrowserOptions(indivConfig!, asset, watch);
146142
return indivConfig;
147143
}
148144

lib/lintplugin/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import type { ESLint } from 'eslint';
22
import * as configs from './configs';
33
import regionComment from './rules/regionComment';
44
import tabType from './rules/tabType';
5-
import collateTypeImports from './rules/typeimports';
65

76
const plugin: ESLint.Plugin = {
87
name: 'Source Academy Lint Plugin',
98
rules: {
10-
// @ts-expect-error typescript-eslint rules are typed differently
11-
'collate-type-imports': collateTypeImports,
129
'tab-type': tabType,
1310
'region-comment': regionComment
1411
},

lib/lintplugin/src/rules/__tests__/tabType.test.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/lintplugin/src/rules/typeimports.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

vitest.config.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
// @ts-check
22
// Root vitest config
3-
import { defineConfig } from 'vitest/config';
3+
import pathlib from 'path';
4+
import { coverageConfigDefaults, defineConfig } from 'vitest/config';
45

56
export default defineConfig({
7+
resolve: {
8+
alias: [{
9+
find: /^js-slang\/context/,
10+
replacement: pathlib.join(import.meta.dirname, 'src/__mocks__/context.ts')
11+
}]
12+
},
613
test: {
714
projects: [
815
'./devserver',
916
'./lib/*',
1017
'./src/bundles/*',
1118
'./src/tabs/*'
12-
]
19+
],
20+
clearMocks: true,
21+
coverage: {
22+
provider: 'v8',
23+
exclude: [
24+
...coverageConfigDefaults.exclude,
25+
'./build/**',
26+
'**/__mocks__/**',
27+
'**/__test_mocks__/**',
28+
'**/bin/**',
29+
'**/build.js',
30+
'**/*.config.?(c)[jt]s',
31+
'**/dist.?(c)js',
32+
'./docs',
33+
'**/src/**/samples/**',
34+
'./lib/buildtools/src/build/docs/drawdown.ts'
35+
]
36+
},
37+
silent: 'passed-only',
1338
}
1439
});

0 commit comments

Comments
 (0)