Skip to content

Commit a5896af

Browse files
committed
feat(test): proper test setup
closes #11
1 parent 2b3a9a1 commit a5896af

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

app-min/test/my-app.spec.ext

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,23 @@ import test from 'tape';
77
// @if mocha
88
import { expect } from 'chai';
99
// @endif
10-
import Aurelia, { CustomElement } from 'aurelia';
10+
import { TestContext, TestConfiguration } from '@aurelia/testing';
11+
import Aurelia/* @if shadow-dom */, { CustomElement }/* @endif */ from 'aurelia';
1112
import { MyApp } from '../src/my-app';
1213

13-
// @if babel
14-
function createAu(template, ...deps) {
15-
// @endif
16-
// @if typescript
17-
function createAu(template: string, ...deps: readonly unknown[]) {
18-
// @endif
19-
const wrapper = CustomElement.define({name: 'wrapper', template});
20-
document.body.appendChild(document.createElement('wrapper'));
21-
return Aurelia.register(deps).app(wrapper);
22-
}
23-
24-
function cleanup() {
25-
const wrapper = document.querySelector('wrapper');
26-
if (wrapper) {
27-
wrapper.remove();
28-
}
29-
}
30-
3114
// @if ava || tape
3215
test('my-app: should render message', async t => {
33-
const au = createAu('<my-app></my-app>', MyApp);
16+
const ctx = TestContext.createHTMLTestContext();
17+
const { container } = ctx;
18+
const node = ctx.createElement('my-app');
19+
20+
const au = new Aurelia(container)
21+
.register(TestConfiguration)
22+
.app({ host: node, component: MyApp });
23+
24+
// const component = au.root.controller.bindingContext;
3425
await au.start().wait();
35-
const node = document.querySelector('my-app');
26+
3627
// @if shadow-dom
3728
// In Shadow DOM open mode, shadowRoot is also accessible through DOM API
3829
// node.shadowRoot
@@ -55,22 +46,25 @@ test('my-app: should render message', async t => {
5546
t.equal(text.trim(), 'Hello World!');
5647
// @endif
5748
await au.stop().wait();
58-
cleanup();
5949
// @if tape
6050
t.end();
6151
// @endif
6252
});
6353
// @endif
6454
// @if jasmine || jest || mocha
6555
describe('my-app', () => {
66-
afterEach(() => {
67-
cleanup();
68-
});
69-
7056
it('should render message', async () => {
71-
const au = createAu('<my-app></my-app>', MyApp);
57+
const ctx = TestContext.createHTMLTestContext();
58+
const { container } = ctx;
59+
const node = ctx.createElement('my-app');
60+
61+
const au = new Aurelia(container)
62+
.register(TestConfiguration)
63+
.app({ host: node, component: MyApp });
64+
65+
// const component = au.root.controller.bindingContext;
7266
await au.start().wait();
73-
const node = document.querySelector('my-app');
67+
7468
// @if shadow-dom
7569
// In Shadow DOM open mode, shadowRoot is also accessible through DOM API
7670
// node.shadowRoot
@@ -93,7 +87,6 @@ describe('my-app', () => {
9387
expect(text.trim()).to.equal('Hello World!');
9488
// @endif
9589
await au.stop().wait();
96-
cleanup();
9790
});
9891
});
9992
// @endif

common/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// @endif
1515
},
1616
"devDependencies": {
17+
// @if !no-unit-tests
18+
"@aurelia/testing": "dev",
19+
// @endif
1720
"htmlhint": "^0.11.0",
1821
// @if less
1922
"lesshint": "^6.0.0",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
HTMLTestContext,
3+
TestContext,
4+
} from '@aurelia/testing';
5+
import {
6+
JitHtmlBrowserConfiguration
7+
} from '@aurelia/jit-html-browser';
8+
import {
9+
BrowserScheduler
10+
} from '@aurelia/runtime-html-browser';
11+
import {
12+
Reporter,
13+
LogLevel,
14+
} from '@aurelia/kernel';
15+
16+
Reporter.level = LogLevel.error;
17+
18+
function createBrowserTestContext()/* @if typescript */: HTMLTestContext/* @endif */ {
19+
return HTMLTestContext.create(
20+
JitHtmlBrowserConfiguration,
21+
window,
22+
BrowserScheduler,
23+
UIEvent,
24+
Event,
25+
CustomEvent,
26+
Node,
27+
Element,
28+
HTMLElement,
29+
HTMLDivElement,
30+
Text,
31+
Comment,
32+
DOMParser,
33+
CSSStyleSheet,
34+
ShadowRoot
35+
);
36+
}
37+
38+
function initializeBrowserTestContext()/* @if typescript */: void/* @endif */ {
39+
TestContext.createHTMLTestContext = createBrowserTestContext;
40+
// Just trigger the HTMLDOM to be resolved once so it sets the DOM globals
41+
TestContext.createHTMLTestContext().dom.createElement('div');
42+
}
43+
44+
initializeBrowserTestContext();

dumber/gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const dr = dumber({
6464
// Kick off all test files.
6565
// Note dumber-module-loader requirejs call accepts regex which loads all matched module ids!
6666
// Note all module ids are relative to dumber option "src" (default to 'src') folder.
67-
isTest && "requirejs([/^\\.\\.\\/test\\/.+\\.spec$/]);"
67+
isTest && "requirejs(['../test/setup', /^\\.\\.\\/test\\/.+\\.spec$/]);"
6868
],
6969
// @endif
7070

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
require('./setup.js');
12
const requireAll = (requireContext) => { requireContext.keys().map(requireContext); };
2-
33
requireAll(require.context('./', true, /spec\.(js|ts)$/));

0 commit comments

Comments
 (0)