Skip to content

Commit 0814e01

Browse files
authored
test: should load external module singleton (#635)
1 parent 2302974 commit 0814e01

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

e2e/singleton/fixtures/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'c';

e2e/singleton/fixtures/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, it } from '@rstest/core';
2+
import { getC } from 'c';
23
import { getA } from './a';
34

45
it('should singleton A', () => {
@@ -9,3 +10,7 @@ it('should singleton B', async () => {
910
const { getB } = await import('./b');
1011
expect(getB()).toBe(process.env.B);
1112
});
13+
14+
it('should singleton C', () => {
15+
expect(getC()).toBe(process.env.C);
16+
});
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { expect, it } from '@rstest/core';
2+
import { getC } from 'c';
23
import { getA } from './a';
34

4-
it('should singleton A - 1', () => {
5+
it('should singleton A', () => {
56
expect(getA()).toBe(process.env.A);
67
});
78

8-
it('should singleton B - 2', async () => {
9+
it('should singleton B', async () => {
910
if (process.env.TestNoIsolate) {
1011
const { getB } = await import('./b');
1112
expect(getB()).toBe(process.env.B);
@@ -14,7 +15,11 @@ it('should singleton B - 2', async () => {
1415
}
1516
});
1617

17-
it('should singleton C', async () => {
18+
it('should singleton B - 1', async () => {
1819
const { getB } = await import('./b');
19-
expect(getB()).toBe(process.env.C);
20+
expect(getB()).toBe(process.env.B1);
21+
});
22+
23+
it('should singleton C', () => {
24+
expect(getC()).toBe(process.env.C);
2025
});

e2e/singleton/fixtures/setup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { beforeAll } from '@rstest/core';
2+
import { getC } from 'c';
23
import { getA } from './a';
34

45
beforeAll(async (context) => {
56
const A = getA();
67

78
process.env.A = A;
9+
process.env.C = getC();
810
const { getB } = await import('./b');
911

1012
const B = getB();
1113
if (context.filepath.includes('index.test')) {
1214
process.env.B = B;
1315
} else {
14-
process.env.C = B;
16+
process.env.B1 = B;
1517
}
1618
});

e2e/singleton/index.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
import { dirname, join } from 'node:path';
22
import { fileURLToPath } from 'node:url';
3-
import { describe, it } from '@rstest/core';
3+
import { beforeAll, describe, it } from '@rstest/core';
4+
import fs from 'fs-extra';
45
import { runRstestCli } from '../scripts/';
56

67
const __filename = fileURLToPath(import.meta.url);
78

89
const __dirname = dirname(__filename);
910

1011
describe('test singleton', () => {
12+
beforeAll(async () => {
13+
fs.ensureDirSync(join(__dirname, 'fixtures', 'node_modules'));
14+
fs.writeFileSync(
15+
join(__dirname, 'fixtures', 'node_modules', 'c.mjs'),
16+
`
17+
let c = undefined;
18+
19+
export const getC = () => {
20+
if (!c) {
21+
c = Math.ceil(Math.random() * 1000).toString();
22+
}
23+
return c;
24+
};
25+
`,
26+
);
27+
});
28+
1129
it('should load singleton module correctly', async () => {
1230
const { expectExecSuccess } = await runRstestCli({
1331
command: 'rstest',

0 commit comments

Comments
 (0)