diff --git a/src/tests/debug-tools-test.js b/src/tests/debug-tools-test.js index 520501a..7fe853f 100644 --- a/src/tests/debug-tools-test.js +++ b/src/tests/debug-tools-test.js @@ -11,465 +11,426 @@ const presets = [["latest", { "es2017": false }]]; -let cases = { - 'Feature Flags': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: false - } - }, - debugTools: { - source: '@ember/debug-tools' - }, - features: [{ - name: "ember-source", - source: '@ember/features', - flags: { - FEATURE_A: false, - FEATURE_B: true - } - }] - }] - ], - }, - fixtures: [ - 'inline-feature-flags', - 'missing-feature-flag' - ], - errors: [ - 'Imported FEATURE_C from @ember/features which is not a supported flag.' - ] - }, - - 'Debug Macros': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - debugTools: { - source: '@ember/debug-tools', - assertPredicateIndex: 0 - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } +describe('Feature Flags', function() { + const h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: false + } + }, + debugTools: { + source: '@ember/debug-tools' + }, + features: [{ + name: "ember-source", + source: '@ember/features', + flags: { + FEATURE_A: false, + FEATURE_B: true } }] - ] - }, - fixtures: [ - 'warn-expansion', - 'assert-expansion', - 'deprecate-expansion', - 'deprecate-missing-id', - 'hygenic-debug-injection', - 'log-expansion' + }] ], - errors: [ - `deprecate's meta information requires an "id" field.`, - `deprecate's meta information requires an "until" field.` + }); + + h.generateTest('inline-feature-flags'); + h.generateErrorTest('missing-feature-flag', 'Imported FEATURE_C from @ember/features which is not a supported flag.'); +}); + +describe('Debug Macros', function(){ + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + debugTools: { + source: '@ember/debug-tools', + assertPredicateIndex: 0 + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true + } + } + }] ] - }, - - 'foreign debug imports': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - externalizeHelpers: { - global: 'Ember' - }, - debugTools: { - source: '@ember/debug-tools', - assertPredicateIndex: 0 - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } + }); + + h.generateTest('warn-expansion'); + h.generateTest('assert-expansion'); + h.generateTest('deprecate-expansion'); + h.generateErrorTest('deprecate-missing-id', `deprecate's meta information requires an "id" field.`); + h.generateTest('hygenic-debug-injection'); + h.generateTest('log-expansion'); +}); + +describe('foreign debug imports', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + externalizeHelpers: { + global: 'Ember' + }, + debugTools: { + source: '@ember/debug-tools', + assertPredicateIndex: 0 + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true } - }], - - [function(babel) { - let t = babel.types; - - return { - name: 'import-remover', - visitor: { - ImportSpecifier(path) { - let importedName = path.node.imported.name; - if (importedName === 'inspect') { - let importDeclarationPath = path.findParent(p => p.isImportDeclaration()); - let binding = path.scope.getBinding(importedName); - let references = binding.referencePaths; - - let replacements = []; - for (let reference of references) { - replacements.push(t.variableDeclaration('var', [ - t.variableDeclarator( - t.identifier(path.node.local.name), - t.memberExpression(t.identifier('Ember'), t.identifier(importedName)) - ), - ])); - } - - path.remove(); - importDeclarationPath.insertAfter(replacements); + } + }], + + [function(babel) { + let t = babel.types; + + return { + name: 'import-remover', + visitor: { + ImportSpecifier(path) { + let importedName = path.node.imported.name; + if (importedName === 'inspect') { + let importDeclarationPath = path.findParent(p => p.isImportDeclaration()); + let binding = path.scope.getBinding(importedName); + let references = binding.referencePaths; + + let replacements = []; + for (let reference of references) { + replacements.push(t.variableDeclaration('var', [ + t.variableDeclarator( + t.identifier(path.node.local.name), + t.memberExpression(t.identifier('Ember'), t.identifier(importedName)) + ), + ])); } + + path.remove(); + importDeclarationPath.insertAfter(replacements); } } } - }] - ] - }, - fixtures: [ - 'shared-debug-module', - ], - errors: [ ] - }, - - 'Global External Test Helpers': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - externalizeHelpers: { - global: '__debugHelpers__' - }, - debugTools: { - source: '@ember/debug-tools' - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } - } - }] - ] - }, + } + }] + ] + }); - fixtures: ['global-external-helpers'] - }, - - 'ember-cli-babel default configuration': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - externalizeHelpers: { - global: 'Ember' - }, - debugTools: { - source: '@ember/debug', - assertPredicateIndex: 1 - }, - envFlags: { - source: '@glimmer/env', - flags: { - DEBUG: true - } + h.generateTest('shared-debug-module'); +}); + +describe('Global External Test Helpers', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + externalizeHelpers: { + global: '__debugHelpers__' + }, + debugTools: { + source: '@ember/debug-tools' + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true } - }] - ] - }, + } + }] + ] + }); - fixtures: ['ember-cli-babel-config'] - }, - - 'Retain Module External Test Helpers': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - externalizeHelpers: { - module: true - }, - debugTools: { - source: '@ember/debug-tools' - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } + h.generateTest('global-external-helpers'); +}); + +describe('ember-cli-babel default configuration', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + externalizeHelpers: { + global: 'Ember' + }, + debugTools: { + source: '@ember/debug', + assertPredicateIndex: 1 + }, + envFlags: { + source: '@glimmer/env', + flags: { + DEBUG: true } - }] - ] - }, + } + }] + ] + }); - fixtures: ['retain-module-external-helpers'] - }, - - 'Development Svelte Builds': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - debugTools: { - source: '@ember/debug-tools' - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } - }, - - svelte: { - 'ember-source': '2.15.0' - }, - - features: [{ - name: 'my-app', - source: 'my-app/features', - flags: { - FEATURE_A: false, - FEATURE_B: true - } - }, - // Note this going to have to be concated in by each lib - { - name: 'ember-source', - source: '@ember/features', - flags: { - DEPRECATED_PARTIALS: '2.14.0', - DEPRECATED_CONTROLLERS: '2.16.0' - } - }] - }] - ] - }, + h.generateTest('ember-cli-babel-config'); +}); - fixtures: ['development-svelte-builds'] - }, - - 'Production Svelte Builds': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - debugTools: { - source: '@ember/debug-tools' - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: false - } - }, - - svelte: { - 'ember-source': '2.15.0' - }, - - features: [{ - name: 'my-app', - source: 'my-app/features', - flags: { - FEATURE_A: false, - FEATURE_B: true - } - }, - // Note this going to have to be concated in by each lib - { - name: 'ember-source', - source: '@ember/features', - flags: { - DEPRECATED_PARTIALS: '2.14.0', - DEPRECATED_CONTROLLERS: '2.16.0' - } - }] - }] - ] - }, +describe('Retain Module External Test Helpers', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + externalizeHelpers: { + module: true + }, + debugTools: { + source: '@ember/debug-tools' + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true + } + } + }] + ] + }); - fixtures: ['production-svelte-builds'] - }, - - 'Inline Env Flags': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true, - TESTING: false - } - }, - debugTools: { - source: '@ember/debug-tools' - }, - features: [] + h.generateTest('retain-module-external-helpers'); +}); + +describe('Development Svelte Builds', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + debugTools: { + source: '@ember/debug-tools' + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true + } + }, + + svelte: { + 'ember-source': '2.15.0' + }, + + features: [{ + name: 'my-app', + source: 'my-app/features', + flags: { + FEATURE_A: false, + FEATURE_B: true + } + }, + // Note this going to have to be concated in by each lib + { + name: 'ember-source', + source: '@ember/features', + flags: { + DEPRECATED_PARTIALS: '2.14.0', + DEPRECATED_CONTROLLERS: '2.16.0' + } }] - ] - }, - fixtures: [ - 'inject-env-flags', - 'debug-flag' + }] ] - }, - - 'Retains non-macro types': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - debugTools: { - source: '@ember/debug-tools' - }, - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } + }); + + h.generateTest('development-svelte-builds'); +}); + +describe('Production Svelte Builds', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + debugTools: { + source: '@ember/debug-tools' + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: false + } + }, + + svelte: { + 'ember-source': '2.15.0' + }, + + features: [{ + name: 'my-app', + source: 'my-app/features', + flags: { + FEATURE_A: false, + FEATURE_B: true + } + }, + // Note this going to have to be concated in by each lib + { + name: 'ember-source', + source: '@ember/features', + flags: { + DEPRECATED_PARTIALS: '2.14.0', + DEPRECATED_CONTROLLERS: '2.16.0' } }] - ] - }, - fixtures: [ - 'retains-import-for-non-macro-types', - 'does-not-modify-non-imported-flags' + }] ] - }, - - 'Runtime Feature Flags': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: false - } - }, - debugTools: { - source: '@ember/debug-tools' - }, - features: { - name: 'ember-source', - source: '@ember/features', - flags: { - FEATURE_A: true, - FEATURE_B: null - } + }); + + h.generateTest('production-svelte-builds'); +}); + +describe('Inline Env Flags', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true, + TESTING: false } - }] - ] - }, - fixtures: [ - 'runtime-feature-flags' + }, + debugTools: { + source: '@ember/debug-tools' + }, + features: [] + }] ] - }, - - 'Runtime default export features': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: false - } - }, - debugTools: { - source: '@ember/debug-tools' - }, - features: { - name: 'ember-source', - source: '@ember/features', - flags: { - FEATURE_A: true, - FEATURE_B: null - } + }); + + h.generateTest('inject-env-flags'); + h.generateTest('debug-flag'); +}); + +describe('Retains non-macro types', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + debugTools: { + source: '@ember/debug-tools' + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true } - }] - ] - }, - fixtures: [ - 'default-export-features' + } + }] ] - }, - - 'Retains runtime feature flag definitions': { - transformOptions: { - presets, - plugins: [ - [DebugToolsPlugin, { - envFlags: { - source: '@ember/env-flags', - flags: { - DEBUG: true - } - }, - debugTools: { - source: '@ember/debug-tools' - }, - features: { - name: 'ember-source', - source: '@ember/features', - flags: { - FOO_BAR: false, - WIDGET_WOO: false - } + }); + + h.generateTest('retains-import-for-non-macro-types'); + h.generateTest('does-not-modify-non-imported-flags'); +}); + +describe('Runtime Feature Flags', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: false } - }] - ] - }, - fixtures: [ - 'retains-runtime-definitions' + }, + debugTools: { + source: '@ember/debug-tools' + }, + features: { + name: 'ember-source', + source: '@ember/features', + flags: { + FEATURE_A: true, + FEATURE_B: null + } + } + }] ] - }, -} + }); + + h.generateTest('runtime-feature-flags'); +}); -Object.keys(cases).forEach(caseName => { - describe(caseName, () => { - let ep = 0; - - let testcase = cases[caseName]; - - testcase.fixtures.forEach(assertionName => { - if (testcase.only) { - it.only(assertionName, () => { - test(testcase, assertionName, ep); - }); - } else if (testcase.skip) { - it.skip(assertionName, () => {}); - } else { - it(assertionName, () => { - test(testcase, assertionName, ep); - }); - } - }); +describe('Runtime default export features', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: false + } + }, + debugTools: { + source: '@ember/debug-tools' + }, + features: { + name: 'ember-source', + source: '@ember/features', + flags: { + FEATURE_A: true, + FEATURE_B: null + } + } + }] + ] }); + + h.generateTest('default-export-features'); }); +describe('Retains runtime feature flag definitions', function() { + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true + } + }, + debugTools: { + source: '@ember/debug-tools' + }, + features: { + name: 'ember-source', + source: '@ember/features', + flags: { + FOO_BAR: false, + WIDGET_WOO: false + } + } + }] + ] + }); -function test(testcase, assertionName, ep) { - let sample = fs.readFileSync(`./fixtures/${assertionName}/sample.js`, 'utf-8'); - let options = testcase.transformOptions; - let expectationPath = `./fixtures/${assertionName}/expectation.js`; - let expectationExists = fs.existsSync(expectationPath); + h.generateTest('retains-runtime-definitions'); +}); - if (expectationExists) { - let expectation = fs.readFileSync(expectationPath, 'utf-8'); - let compiled = transform(sample, options); - expect(compiled.code).to.equal(expectation); +function transformTestHelper(options) { + return { + generateTest(fixtureName) { + it(fixtureName, function() { + let sample = fs.readFileSync(`./fixtures/${fixtureName}/sample.js`, 'utf-8'); + let expectation = fs.readFileSync(`./fixtures/${fixtureName}/expectation.js`, 'utf-8'); + expect(transform(sample, options).code).to.equal(expectation); + }); + }, - } else { - let fn = () => transform(sample, options); - expect(fn).to.throw(new RegExp(testcase.errors[ep++])); - } + generateErrorTest(fixtureName, error) { + it(fixtureName, function() { + let sample = fs.readFileSync(`./fixtures/${fixtureName}/sample.js`, 'utf-8'); + expect(() => transform(sample, options)).to.throw(error); + }); + }, + }; }