Skip to content

Commit 4cedd3e

Browse files
Merge pull request #21169 from emberjs/nvp/migrate-node-tests-to-esm
Migrate smoke tests to use ESM Ember -- ES'Mber
2 parents b721b22 + 6829c8e commit 4cedd3e

15 files changed

+213
-508
lines changed

smoke-tests/node-template/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "ember-test-node-template",
33
"version": "0.0.0",
44
"private": true,
5+
"type": "module",
56
"description": "Node-focused smoke test template for ember-source",
67
"scripts": {
78
"test:node": "qunit tests/node/**/*-test.js"

smoke-tests/node-template/tests/node/app-boot-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const setupAppTest = require('./helpers/setup-app');
1+
import setupAppTest from './helpers/setup-app.js';
2+
import { register } from './helpers/assert-html-matches.js';
23

3-
require('./helpers/assert-html-matches').register();
4+
register();
45

56
QUnit.module('App Boot', function (hooks) {
67
setupAppTest(hooks);

smoke-tests/node-template/tests/node/component-rendering-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const setupComponentTest = require('./helpers/setup-component');
1+
import setupComponentTest from './helpers/setup-component.js';
22

33
QUnit.module('Components can be rendered without a DOM dependency', function (hooks) {
44
setupComponentTest(hooks);
@@ -22,7 +22,7 @@ QUnit.module('Components can be rendered without a DOM dependency', function (ho
2222
function (assert) {
2323
this.owner.register(
2424
'component:fake-link',
25-
class extends this.Ember.Component {
25+
class extends this.Component {
2626
tagName = 'link';
2727
attributeBindings = ['href', 'rel'];
2828
rel = 'canonical';

smoke-tests/node-template/tests/node/fastboot-sandbox-test.js

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
const fs = require('fs');
2-
const vm = require('vm');
3-
const SimpleDOM = require('simple-dom');
4-
const { emberPath, loadEmber, clearEmber } = require('./helpers/load-ember');
1+
import SimpleDOM from 'simple-dom';
2+
import Application from 'ember-source/@ember/application/index.js';
3+
import EmberRouter from 'ember-source/@ember/routing/router.js';
4+
import { run } from 'ember-source/@ember/runloop/index.js';
5+
import { precompile } from 'ember-source/ember-template-compiler/index.js';
6+
import { createTemplateFactory } from 'ember-source/@ember/template-factory/index.js';
7+
8+
function compile(templateString, options) {
9+
let templateSpec = precompile(templateString, options);
10+
return createTemplateFactory(JSON.parse(templateSpec));
11+
}
512

613
// This is based on what fastboot-server does
714
let HTMLSerializer = new SimpleDOM.HTMLSerializer(SimpleDOM.voidMap);
815

9-
async function fastbootVisit(context, url) {
16+
async function fastbootVisit(app, url) {
1017
let doc = new SimpleDOM.Document();
1118
let rootElement = doc.body;
1219
let options = { isBrowser: false, document: doc, rootElement: rootElement };
1320

14-
let { app } = context;
15-
1621
await app.boot();
1722

1823
let instance = await app.buildInstance();
@@ -31,84 +36,40 @@ async function fastbootVisit(context, url) {
3136
}
3237
}
3338

34-
// essentially doing the same as what is done in FastBoot 3.1.0
35-
// https://github.com/ember-fastboot/fastboot/blob/v3.1.0/src/sandbox.js
36-
function buildSandboxContext(precompile) {
37-
let URL = require('url');
38-
39-
let sandbox = {
40-
console,
41-
setTimeout,
42-
clearTimeout,
43-
URL,
44-
45-
// Convince jQuery not to assume it's in a browser
46-
module: { exports: {}, require() {} },
47-
};
48-
49-
// Set the global as `window`
50-
sandbox.window = sandbox;
51-
sandbox.window.self = sandbox;
52-
53-
let context = vm.createContext(sandbox);
54-
55-
let environmentSetupScript = new vm.Script(
56-
`
57-
var EmberENV = {
58-
_DEFAULT_ASYNC_OBSERVERS: true,
59-
_JQUERY_INTEGRATION: false,
60-
};`,
61-
{ filename: 'prepend.js' }
62-
);
63-
environmentSetupScript.runInContext(context);
64-
65-
let emberSource = fs.readFileSync(emberPath, { encoding: 'utf-8' });
66-
let emberScript = new vm.Script(emberSource, { filename: emberPath });
67-
emberScript.runInContext(context);
68-
69-
let applicationSource = `
70-
let Ember = module.exports;
71-
72-
class Router extends Ember.Router {}
73-
Router.map(function() {
74-
this.route('a');
75-
this.route('b');
76-
});
77-
78-
const registry = {
79-
'router:main': Router,
80-
'template:application': ${precompile('<h1>Hello world!</h1>\n{{outlet}}')}
81-
};
82-
83-
class Resolver extends Ember.Object {
84-
resolve(specifier) {
85-
return registry[specifier];
86-
}
87-
}
88-
89-
var app = class extends Ember.Application {}.create({
90-
autoboot: false,
91-
Resolver,
92-
});
93-
`;
94-
let appScript = new vm.Script(applicationSource, { filename: 'app.js' });
95-
appScript.runInContext(context);
96-
97-
return context;
98-
}
99-
10039
QUnit.module('Ember.Application - visit() Integration Tests', function (hooks) {
101-
hooks.beforeEach(function () {
102-
let { precompile } = loadEmber();
103-
this.context = buildSandboxContext(precompile);
104-
});
40+
let app;
10541

10642
hooks.afterEach(function () {
107-
clearEmber();
43+
if (app) {
44+
run(app, 'destroy');
45+
app = null;
46+
}
10847
});
10948

11049
QUnit.test('FastBoot: basic', async function (assert) {
111-
let result = await fastbootVisit(this.context, '/');
50+
let Router = class extends EmberRouter {};
51+
Router.map(function () {
52+
this.route('a');
53+
this.route('b');
54+
});
55+
56+
let registry = {
57+
'router:main': Router,
58+
'template:application': compile('<h1>Hello world!</h1>\n{{outlet}}'),
59+
};
60+
61+
app = class extends Application {}.create({
62+
autoboot: false,
63+
Resolver: {
64+
create: () => ({
65+
resolve(specifier) {
66+
return registry[specifier];
67+
},
68+
}),
69+
},
70+
});
71+
72+
let result = await fastbootVisit(app, '/');
11273

11374
assert.equal(result.url, '/', 'landed on correct url');
11475
assert.equal(

smoke-tests/node-template/tests/node/fixtures/project.js

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,3 @@
1-
module.exports = class Project {
2-
static withDep(depOptions = {}, projectOptions = {}) {
3-
let addons = projectOptions.addons || [];
4-
5-
return new Project({
6-
...projectOptions,
7-
addons: [...addons, new Addon(depOptions)],
8-
});
9-
}
10-
11-
static withTransientDep(transientDepOptions = {}, depOptions = {}, projectOptions = {}) {
12-
let addons = depOptions.addons || [];
13-
14-
return Project.withDep(
15-
{
16-
...depOptions,
17-
addons: [
18-
...addons,
19-
new Addon({
20-
name: 'my-nested-addon',
21-
version: '0.1.0',
22-
...transientDepOptions,
23-
}),
24-
],
25-
},
26-
projectOptions
27-
);
28-
}
29-
30-
constructor({
31-
name = 'my-app',
32-
emberCliBabel,
33-
dependencies = {},
34-
devDependencies = {},
35-
addons = [],
36-
} = {}) {
37-
this.name = () => name;
38-
this.parent = null;
39-
this.pkg = {
40-
name,
41-
dependencies: { ...dependencies },
42-
devDependencies: { ...devDependencies },
43-
};
44-
this.addons = [...addons];
45-
46-
if (typeof emberCliBabel === 'string') {
47-
this.pkg.devDependencies['ember-cli-babel'] = emberCliBabel;
48-
}
49-
50-
reifyAddons(this);
51-
addMissingAddons(this, this.pkg.devDependencies);
52-
addMissingAddons(this, this.pkg.dependencies);
53-
addMissingDeps(this, true);
54-
}
55-
};
56-
571
class Addon {
582
constructor({
593
parent,
@@ -146,3 +90,59 @@ function addMissingDeps(parent, devDeps = false) {
14690
}
14791
}
14892
}
93+
94+
export default class Project {
95+
static withDep(depOptions = {}, projectOptions = {}) {
96+
let addons = projectOptions.addons || [];
97+
98+
return new Project({
99+
...projectOptions,
100+
addons: [...addons, new Addon(depOptions)],
101+
});
102+
}
103+
104+
static withTransientDep(transientDepOptions = {}, depOptions = {}, projectOptions = {}) {
105+
let addons = depOptions.addons || [];
106+
107+
return Project.withDep(
108+
{
109+
...depOptions,
110+
addons: [
111+
...addons,
112+
new Addon({
113+
name: 'my-nested-addon',
114+
version: '0.1.0',
115+
...transientDepOptions,
116+
}),
117+
],
118+
},
119+
projectOptions
120+
);
121+
}
122+
123+
constructor({
124+
name = 'my-app',
125+
emberCliBabel,
126+
dependencies = {},
127+
devDependencies = {},
128+
addons = [],
129+
} = {}) {
130+
this.name = () => name;
131+
this.parent = null;
132+
this.pkg = {
133+
name,
134+
dependencies: { ...dependencies },
135+
devDependencies: { ...devDependencies },
136+
};
137+
this.addons = [...addons];
138+
139+
if (typeof emberCliBabel === 'string') {
140+
this.pkg.devDependencies['ember-cli-babel'] = emberCliBabel;
141+
}
142+
143+
reifyAddons(this);
144+
addMissingAddons(this, this.pkg.devDependencies);
145+
addMissingAddons(this, this.pkg.dependencies);
146+
addMissingDeps(this, true);
147+
}
148+
}
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
const { HtmlDiffer } = require('html-differ');
1+
import { HtmlDiffer } from 'html-differ';
2+
import QUnit from 'qunit';
23

34
const htmlDiffer = new HtmlDiffer({
45
ignoreAttributes: ['id'],
56
ignoreWhitespaces: true,
67
});
78

8-
module.exports = {
9-
/*
10-
* This assertion helper tests whether two fragments of Html 'appear'
11-
* to match. In terms of fragments rendered by Ember, we want to explicitly
12-
* ignore whitespace and certain attributes values, such as IDs, which Ember
13-
* auto-generates. Attribute ordering is also ignored.
14-
*/
15-
register() {
16-
QUnit.assert.htmlMatches = function (actual, expected, message) {
17-
let isEqual = htmlDiffer.isEqual(actual, expected);
9+
/*
10+
* This assertion helper tests whether two fragments of Html 'appear'
11+
* to match. In terms of fragments rendered by Ember, we want to explicitly
12+
* ignore whitespace and certain attributes values, such as IDs, which Ember
13+
* auto-generates. Attribute ordering is also ignored.
14+
*/
15+
export function register() {
16+
QUnit.assert.htmlMatches = function (actual, expected, message) {
17+
let isEqual = htmlDiffer.isEqual(actual, expected);
1818

19-
this.pushResult({
20-
result: isEqual,
21-
actual,
22-
expected,
23-
message,
24-
});
25-
};
26-
},
27-
};
19+
this.pushResult({
20+
result: isEqual,
21+
actual,
22+
expected,
23+
message,
24+
});
25+
};
26+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
module.exports = function buildOwner(Ember, resolver) {
2-
let Owner = Ember.Object.extend(Ember._RegistryProxyMixin, Ember._ContainerProxyMixin);
1+
import EmberObject from 'ember-source/@ember/object/index.js';
2+
import Application from 'ember-source/@ember/application/index.js';
3+
import ApplicationInstance from 'ember-source/@ember/application/instance.js';
4+
import RegistryProxyMixin from 'ember-source/@ember/-internals/runtime/lib/mixins/registry_proxy.js';
5+
import ContainerProxyMixin from 'ember-source/@ember/-internals/runtime/lib/mixins/container_proxy.js';
6+
import { Registry } from 'ember-source/@ember/-internals/container/index.js';
37

4-
let namespace = Ember.Object.create({
8+
export default function buildOwner(resolver) {
9+
let Owner = EmberObject.extend(RegistryProxyMixin, ContainerProxyMixin);
10+
11+
let namespace = EmberObject.create({
512
Resolver: {
613
create: function () {
714
return resolver;
815
},
916
},
1017
});
1118

12-
let fallbackRegistry = Ember.Application.buildRegistry(namespace);
13-
let registry = new Ember.Registry({
19+
let fallbackRegistry = Application.buildRegistry(namespace);
20+
let registry = new Registry({
1421
fallback: fallbackRegistry,
1522
});
1623

17-
Ember.ApplicationInstance.setupRegistry(registry);
24+
ApplicationInstance.setupRegistry(registry);
1825

1926
let owner = Owner.create({
2027
__registry__: registry,
@@ -25,4 +32,4 @@ module.exports = function buildOwner(Ember, resolver) {
2532
owner.__container__ = container;
2633

2734
return owner;
28-
};
35+
}

0 commit comments

Comments
 (0)