Skip to content

Commit

Permalink
adding test case
Browse files Browse the repository at this point in the history
  • Loading branch information
thoov committed Aug 4, 2021
1 parent ac41096 commit 0ea82c1
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function excludeDotFiles(files: string[]) {
}

export const CACHE_BUSTING_PLUGIN = {
path: require.resolve('./babel-plugin-cache-busting'),
path: require.resolve('@embroider/shared-internals/src/babel-plugin-cache-busting.js'),
version: readJSONSync(`${__dirname}/../package.json`).version,
};

Expand Down
3 changes: 0 additions & 3 deletions packages/macros/src/babel-plugin-cache-busting.ts

This file was deleted.

9 changes: 8 additions & 1 deletion packages/macros/src/ember-addon-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ export = {

let yarnLockPath = join(appInstance.project.root, 'yarn.lock');
let npmLockPath = join(appInstance.project.root, 'package-lock.json');
let pnpmLockPath = join(appInstance.project.root, 'pnpm-lock.yaml');
let packagePath = join(appInstance.project.root, 'package.json');
let lockFileBuffer;

if (fs.existsSync(yarnLockPath)) {
lockFileBuffer = fs.readFileSync(yarnLockPath);
} else if (fs.existsSync(npmLockPath)) {
lockFileBuffer = fs.readFileSync(npmLockPath);
} else if (fs.existsSync(pnpmLockPath)) {
lockFileBuffer = fs.readFileSync(pnpmLockPath);
} else {
// no lock file found, using package.json as a fall back
lockFileBuffer = fs.readFileSync(packagePath);
Expand All @@ -95,7 +98,11 @@ export = {
// hash representing the lock file of the app and if it ever changes forces babel to rerun its plugins.
// more information in issue #906
let cacheKey = crypto.createHash('sha256').update(lockFileBuffer).digest('hex');
babelPlugins.push([require.resolve('./babel-plugin-cache-busting'), { version: cacheKey }]);
babelPlugins.push([
require.resolve('@embroider/shared-internals/src/babel-plugin-cache-busting.js'),
{ version: cacheKey },
'@embroider/macros cache buster',
]);
}
},

Expand Down
3 changes: 2 additions & 1 deletion packages/shared-internals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
".": {
"browser": "./src/browser-index.js",
"default": "./src/index.js"
}
},
"./src/babel-plugin-cache-busting.js": "./src/babel-plugin-cache-busting.js"
},
"license": "MIT",
"author": "Edward Faulkner",
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/macro-test/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import Controller from '@ember/controller';
import { getOwnConfig, isTesting, isDevelopingApp } from '@embroider/macros';
import { getOwnConfig, isTesting, isDevelopingApp, macroCondition, dependencySatisfies } from '@embroider/macros';

export default class Application extends Controller {
constructor() {
super(...arguments);
this.mode = getOwnConfig()['mode'];
this.isTesting = isTesting();
this.isDeveloping = isDevelopingApp();

if (macroCondition(dependencySatisfies('lodash', '^4'))) {
this.lodashVersion = 'four';
} else {
this.lodashVersion = 'three';
}
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/macro-test/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div data-test-mode>{{this.mode}}</div>
<div data-test-count>{{macroGetOwnConfig "count"}}</div>
<div>isDeveloping: <span data-test-developing>{{this.isDeveloping}}</span></div>
<div>isTesting: <span data-test-testing>{{this.isTesting}}</span></div>
<div>isTesting: <span data-test-testing>{{this.isTesting}}</span></div>
<div data-test-version>{{this.lodashVersion}}</div>
52 changes: 52 additions & 0 deletions tests/fixtures/macro-test/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

module.exports = function (environment) {
let ENV = {
modulePrefix: 'app-template',
environment,
rootURL: '/',
locationType: 'auto',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. EMBER_NATIVE_DECORATOR_SUPPORT: true
},
EXTEND_PROTOTYPES: {
// Prevent Ember Data from overriding Date.parse.
Date: false,
},
},

APP: {
// Here you can pass flags/options to your application instance
// when it is created
},
LODASH_VERSION: process.env.LODASH_VERSION || 'four',
};

if (environment === 'development') {
// ENV.APP.LOG_RESOLVER = true;
// ENV.APP.LOG_ACTIVE_GENERATION = true;
// ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
}

if (environment === 'test') {
// Testem prefers this...
ENV.locationType = 'none';

// keep test console output quieter
ENV.APP.LOG_ACTIVE_GENERATION = false;
ENV.APP.LOG_VIEW_LOOKUPS = false;

ENV.APP.rootElement = '#ember-testing';
ENV.APP.autoboot = false;
}

if (environment === 'production') {
// here you can enable a production-specific feature
}

return ENV;
};
21 changes: 15 additions & 6 deletions tests/fixtures/macro-test/tests/acceptance/basic-test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import ENV from 'app-template/config/environment';

module('Acceptance | smoke tests', function(hooks) {
module('Acceptance | smoke tests', function (hooks) {
setupApplicationTest(hooks);

test('ensure all scripts in index.html 200', async function(assert) {
test('ensure all scripts in index.html 200', async function (assert) {
for (let { src } of document.scripts) {
let { status } = await fetch(src);
assert.equal(status, 200, `expected: '${src}' to be accessible`);
}
});

test('JS getOwnConfig worked', async function(assert) {
test('JS getOwnConfig worked', async function (assert) {
await visit('/');
assert.equal(currentURL(), '/');
assert.equal(this.element.querySelector('[data-test-mode]').textContent.trim(), 'amazing');
});

test('HBS getOwnConfig worked', async function(assert) {
test('HBS getOwnConfig worked', async function (assert) {
await visit('/');
assert.equal(currentURL(), '/');
assert.equal(this.element.querySelector('[data-test-count]').textContent.trim(), '42');
});

test('JS isTesting worked', async function(assert) {
test('JS isTesting worked', async function (assert) {
await visit('/');
assert.equal(currentURL(), '/');
assert.equal(this.element.querySelector('[data-test-testing]').textContent.trim(), 'true');
});

test('/ordered.js is ordered correctly', function(assert) {
test('/ordered.js is ordered correctly', function (assert) {
assert.deepEqual(self.ORDER, [
// these come via app.import(name, { prepend: true });
// which ultimately end up in vendor.js
Expand All @@ -49,4 +50,12 @@ module('Acceptance | smoke tests', function(hooks) {
'ONE',
]);
});

test('foobar', async function (assert) {
await visit('/');
assert.equal(currentURL(), '/');

let expectedVersion = ENV.LODASH_VERSION;
assert.equal(this.element.querySelector('[data-test-version]').textContent.trim(), expectedVersion);
});
});
28 changes: 27 additions & 1 deletion tests/scenarios/macro-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { appScenarios } from './scenarios';
import { PreparedApp, Project } from 'scenario-tester';
import QUnit from 'qunit';
import merge from 'lodash/merge';
import { dirname } from 'path';
import { dirname, join } from 'path';
import { loadFromFixtureData } from './helpers';
import fs from 'fs-extra';
const { module: Qmodule, test } = QUnit;

appScenarios
Expand All @@ -27,6 +28,7 @@ appScenarios
funkySampleAddon.linkDependency('@embroider/macros', { baseDir: __dirname });
macroSampleAddon.linkDependency('@embroider/macros', { baseDir: __dirname });
project.linkDevDependency('@embroider/macros', { baseDir: __dirname });
project.linkDevDependency('lodash', { baseDir: __dirname });

project.addDevDependency(macroSampleAddon);
project.addDevDependency(funkySampleAddon);
Expand All @@ -53,5 +55,29 @@ appScenarios
let result = await app.execute(`cross-env THROW_UNLESS_PARALLELIZABLE=1 CLASSIC=true yarn test`);
assert.equal(result.exitCode, 0, result.output);
});

test(`multiple dependency check`, async function (assert) {
let pkgJson = fs.readJsonSync(join(app.dir, 'package.json'));
let pkgJsonLodash = fs.readJsonSync(join(app.dir, 'node_modules', 'lodash', 'package.json'));

pkgJson.devDependencies.lodash = '4.0.0';
pkgJsonLodash.version = '4.0.0';

fs.writeJsonSync(join(app.dir, 'package.json'), pkgJson);
fs.writeJsonSync(join(app.dir, 'node_modules', 'lodash', 'package.json'), pkgJsonLodash);

let lodashFourRun = await app.execute(`yarn test`);
assert.equal(lodashFourRun.exitCode, 0, lodashFourRun.output);

// downgrade lodash to 3.0.0
pkgJson.devDependencies.lodash = '3.0.0';
pkgJsonLodash.version = '3.0.0';

fs.writeJsonSync(join(app.dir, 'package.json'), pkgJson);
fs.writeJsonSync(join(app.dir, 'node_modules', 'lodash', 'package.json'), pkgJsonLodash);

let lodashThreeRun = await app.execute(`cross-env LODASH_VERSION=three yarn test`);
assert.equal(lodashThreeRun.exitCode, 0, lodashThreeRun.output);
});
});
});
7 changes: 4 additions & 3 deletions tests/scenarios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"@types/qunit": "^2.11.1",
"fastboot": "^3.1.0",
"fs-extra": "^10.0.0",
"globby": "^11.0.3",
"jsdom": "^16.2.2",
"lodash": "^4.17.20",
Expand All @@ -29,20 +30,20 @@
"ember-cli-3.20": "npm:ember-cli@~3.20.0",
"ember-cli-3.24": "npm:ember-cli@~3.24.0",
"ember-cli-beta": "npm:ember-cli@beta",
"ember-cli-latest": "npm:ember-cli@latest",
"ember-cli-fastboot": "^2.2.3",
"ember-cli-latest": "npm:ember-cli@latest",
"ember-composable-helpers": "^4.4.1",
"ember-data-3.16": "npm:ember-data@~3.16.0",
"ember-data-3.20": "npm:ember-data@~3.20.0",
"ember-data-3.24": "npm:ember-data@~3.24.0",
"ember-data-latest": "npm:ember-data@latest",
"ember-engines": "^0.8.16",
"ember-inline-svg": "^0.2.1",
"ember-source-latest": "npm:ember-source@latest",
"ember-source-beta": "npm:ember-source@beta",
"ember-source-3.16": "npm:ember-source@~3.16.0",
"ember-source-3.20": "npm:ember-source@~3.20.0",
"ember-source-3.24": "npm:ember-source@~3.24.0",
"ember-source-beta": "npm:ember-source@beta",
"ember-source-latest": "npm:ember-source@latest",
"ember-truth-helpers": "^3.0.0",
"fastboot-addon": "link:../../test-packages/fastboot-addon"
},
Expand Down
38 changes: 26 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5673,15 +5673,15 @@ browserify-zlib@^0.2.0:
pako "~1.0.5"

browserslist@^3.2.6, browserslist@^4.0.0, browserslist@^4.14.0, browserslist@^4.14.5, browserslist@^4.16.6:
version "4.16.6"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.16.6.tgz#d7901277a5a88e554ed305b183ec9b0c08f66fa2"
integrity sha512-Wspk/PqO+4W9qp5iUTJsa1B/QrYn1keNCcEP5OvP7WBwT4KaDly0uONYmC6Xa3Z5IqnUgS0KcgLYu1l74x0ZXQ==
version "4.16.7"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.16.7.tgz#108b0d1ef33c4af1b587c54f390e7041178e4335"
integrity sha512-7I4qVwqZltJ7j37wObBe3SoTz+nS8APaNcrBOlgoirb6/HbEU2XxW/LpUDTCngM6iauwFqmRTuOMfyKnFGY5JA==
dependencies:
caniuse-lite "^1.0.30001219"
caniuse-lite "^1.0.30001248"
colorette "^1.2.2"
electron-to-chromium "^1.3.723"
electron-to-chromium "^1.3.793"
escalade "^3.1.1"
node-releases "^1.1.71"
node-releases "^1.1.73"

bser@2.1.1:
version "2.1.1"
Expand Down Expand Up @@ -5917,11 +5917,16 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001219:
caniuse-lite@^1.0.0:
version "1.0.30001236"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001236.tgz#0a80de4cdf62e1770bb46a30d884fc8d633e3958"
integrity sha512-o0PRQSrSCGJKCPZcgMzl5fUaj5xHe8qA2m4QRvnyY4e1lITqoNkr7q/Oh1NcpGSy0Th97UZ35yoKcINPoq7YOQ==

caniuse-lite@^1.0.30001248:
version "1.0.30001248"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001248.tgz#26ab45e340f155ea5da2920dadb76a533cb8ebce"
integrity sha512-NwlQbJkxUFJ8nMErnGtT0QTM2TJ33xgz4KXJSMIrjXIbDVdaYueGyjOrLKRtJC+rTiWfi6j5cnZN1NBiSBJGNw==

capture-exit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
Expand Down Expand Up @@ -7181,10 +7186,10 @@ ee-first@1.1.1:
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

electron-to-chromium@^1.3.723:
version "1.3.752"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.752.tgz#0728587f1b9b970ec9ffad932496429aef750d09"
integrity sha512-2Tg+7jSl3oPxgsBsWKh5H83QazTkmWG/cnNwJplmyZc7KcN61+I10oUgaXSVk/NwfvN3BdkKDR4FYuRBQQ2v0A==
electron-to-chromium@^1.3.793:
version "1.3.796"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.796.tgz#bd74a4367902c9d432d129f265bf4542cddd9f54"
integrity sha512-agwJFgM0FUC1UPPbQ4aII3HamaaJ09fqWGAWYHmzxDWqdmTleCHyyA0kt3fJlTd5M440IaeuBfzXzXzCotnZcQ==

elliptic@^6.5.3:
version "6.5.4"
Expand Down Expand Up @@ -10982,6 +10987,15 @@ fs-extra@^0.30.0:
path-is-absolute "^1.0.0"
rimraf "^2.2.8"

fs-extra@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.0.0.tgz#9ff61b655dde53fb34a82df84bb214ce802e17c1"
integrity sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-extra@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-3.0.1.tgz#3794f378c58b342ea7dbbb23095109c4b3b62291"
Expand Down Expand Up @@ -14563,7 +14577,7 @@ node-notifier@^9.0.1:
uuid "^8.3.0"
which "^2.0.2"

node-releases@^1.1.71:
node-releases@^1.1.73:
version "1.1.73"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.73.tgz#dd4e81ddd5277ff846b80b52bb40c49edf7a7b20"
integrity sha512-uW7fodD6pyW2FZNZnp/Z3hvWKeEW1Y8R1+1CnErE8cXFXzl5blBOoVB41CvMer6P6Q0S5FXDwcHgFd1Wj0U9zg==
Expand Down

0 comments on commit 0ea82c1

Please sign in to comment.