Skip to content

Commit 3c2cb62

Browse files
committed
fix: fix au2 mocha setup
1 parent 8b734f5 commit 3c2cb62

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

client/src/skeletons/aurelia2.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const appTs = `export class MyApp {
5050
}
5151
`;
5252

53-
const testSetup = ext => `import { BrowserPlatform } from '@aurelia/platform-browser';
53+
const jasmineSetup = ext => `import { BrowserPlatform } from '@aurelia/platform-browser';
5454
import { setPlatform, onFixtureCreated${ext === '.ts' ? ', type IFixture' : ''} } from '@aurelia/testing';
5555
5656
// Sets up the Aurelia environment for testing
@@ -80,6 +80,39 @@ afterEach(() => {
8080
});
8181
`;
8282

83+
const mochaSetup = ext => `import { BrowserPlatform } from '@aurelia/platform-browser';
84+
import { setPlatform, onFixtureCreated${ext === '.ts' ? ', type IFixture' : ''} } from '@aurelia/testing';
85+
86+
// Sets up the Aurelia environment for testing
87+
function bootstrapTextEnv() {
88+
const platform = new BrowserPlatform(window);
89+
setPlatform(platform);
90+
BrowserPlatform.set(globalThis, platform);
91+
}
92+
93+
const fixtures${ext === '.ts' ? ': IFixture<object>[]' : ''} = [];
94+
mocha.setup({
95+
rootHooks: {
96+
beforeAll() {
97+
bootstrapTextEnv();
98+
onFixtureCreated(fixture => {
99+
fixtures.push(fixture);
100+
});
101+
},
102+
afterEach() {
103+
fixtures.forEach(async f => {
104+
try {
105+
await f.stop(true);
106+
} catch {
107+
// ignore
108+
}
109+
});
110+
fixtures.length = 0;
111+
}
112+
}
113+
});
114+
`;
115+
83116
const jasmineTest = `import { createFixture } from '@aurelia/testing';
84117
import { MyApp } from '../src/my-app';
85118
@@ -137,23 +170,26 @@ export default function({transpiler, testFramework}) {
137170
}
138171
];
139172

140-
if (testFramework !== 'none') {
141-
files.push({
142-
filename: `test/setup${ext}`,
143-
content: testSetup(ext)
144-
});
145-
146-
if (testFramework === 'jasmine') {
147-
files.push({
173+
if (testFramework === 'jasmine') {
174+
files.push(
175+
{
176+
filename: `test/setup${ext}`,
177+
content: jasmineSetup(ext)
178+
}, {
148179
filename: `test/app.spec${ext}`,
149180
content: jasmineTest
150-
});
151-
} else if (testFramework === 'mocha') {
152-
files.push({
181+
}
182+
);
183+
} else if (testFramework === 'mocha') {
184+
files.push(
185+
{
186+
filename: `test/setup${ext}`,
187+
content: mochaSetup(ext)
188+
}, {
153189
filename: `test/app.spec${ext}`,
154190
content: mochaTest
155-
});
156-
}
191+
}
192+
);
157193
}
158194

159195
return files;

client/src/skeletons/skeleton-generator.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ const DEFAULT_MOCHA_INDEX_HTML = `<!DOCTYPE html>
6060
<head>
6161
<meta charset="utf-8">
6262
<title>Unit Tests</title>
63-
<link rel="stylesheet" href="//${JSDELIVR_CDN_DOMAIN}/npm/mocha@7/mocha.css">
63+
<link rel="stylesheet" href="//${JSDELIVR_CDN_DOMAIN}/npm/mocha/mocha.css">
6464
</head>
6565
<body>
6666
<div id="mocha"></div>
67-
<script src="//${JSDELIVR_CDN_DOMAIN}/npm/mocha@7/mocha.js"></script>
67+
<script src="//${JSDELIVR_CDN_DOMAIN}/npm/mocha/mocha.js"></script>
6868
<script class="mocha-init">
69-
mocha.setup({ui: "bdd", reporter: "html"});
69+
mocha.setup('bdd');
70+
mocha.checkLeaks();
7071
</script>
7172
<script src="/dist/entry-bundle.js"></script>
7273
<script>

0 commit comments

Comments
 (0)