diff --git a/build-system/tasks/runtime-test/runtime-test-base.js b/build-system/tasks/runtime-test/runtime-test-base.js index 47f64dfff24c..5a7f0bc7850a 100644 --- a/build-system/tasks/runtime-test/runtime-test-base.js +++ b/build-system/tasks/runtime-test/runtime-test-base.js @@ -127,7 +127,7 @@ function getFiles(testType) { switch (testType) { case 'unit': - files = testConfig.commonUnitTestPaths.concat(testConfig.chaiAsPromised); + files = testConfig.commonUnitTestPaths; if (argv.files) { return files.concat(argv.files); } @@ -147,7 +147,7 @@ function getFiles(testType) { return files.concat(testConfig.integrationTestPaths); case 'a4a': - return testConfig.chaiAsPromised.concat(testConfig.a4aTestPaths); + return testConfig.a4aTestPaths; default: throw new Error(`Test type ${testType} was not recognized`); @@ -241,8 +241,31 @@ class RuntimeTestConfig { // don't overwrite existing plugins const plugins = [instanbulPlugin].concat(this.babelifyConfig.plugins); + /** + * 'chai-as-promised' contains ES6 code. This is fine for most test environments, + * but not for integration tests running on SauceLabs, since some browsers only + * support ES5 code. Therefore we instruct browserify to transpile the library. + * + * Since browserify doesn't have good support for specifying a node_module to transpile, + * this hack works by saying "transpile everything except for thing in node_modules + * called 'chai-as-promised'". + */ + const babelifyChaiAsPromisedHack = + this.testType !== 'integration' + ? {} + : { + global: true, + ignore: [/\/node_modules\/(?!chai-as-promised\/)/], + }; this.browserify.transform = [ - ['babelify', {...this.babelifyConfig, plugins}], + [ + 'babelify', + { + ...this.babelifyConfig, + ...babelifyChaiAsPromisedHack, + plugins, + }, + ], ]; } } diff --git a/build-system/test-configs/config.js b/build-system/test-configs/config.js index dd4497b29560..18e255e4f9a2 100644 --- a/build-system/test-configs/config.js +++ b/build-system/test-configs/config.js @@ -73,8 +73,6 @@ const a4aTestPaths = initTestsPath.concat([ 'ads/google/a4a/test/*.js', ]); -const chaiAsPromised = ['test/chai-as-promised/chai-as-promised.js']; - const unitTestPaths = [ 'test/unit/**/*.js', 'ads/**/test/test-*.js', @@ -204,7 +202,6 @@ const changelogIgnoreFileTypes = /\.md|\.json|\.yaml|LICENSE|CONTRIBUTORS$/; /** @const */ module.exports = { a4aTestPaths, - chaiAsPromised, changelogIgnoreFileTypes, commonIntegrationTestPaths, commonUnitTestPaths, diff --git a/extensions/amp-bind/0.1/test/integration/test-bind-impl.js b/extensions/amp-bind/0.1/test/integration/test-bind-impl.js index f7df9d6fe2f9..5a9a068cf867 100644 --- a/extensions/amp-bind/0.1/test/integration/test-bind-impl.js +++ b/extensions/amp-bind/0.1/test/integration/test-bind-impl.js @@ -983,13 +983,8 @@ describe mystate: {mykey: 'myval'}, }); - // Integration tests may not use chai-as-promised. - try { - await bind.getStateAsync('mystate.mykey'); - expect.fail(); - } catch (err) { - expect(err).match(/#mystate/); - } + const state = bind.getStateAsync('mystate.mykey'); + expect(state).to.eventually.rejectedWith(/#mystate/); }); it('should not wait if the still-loading state is irrelevant', async () => { diff --git a/test/_init_tests.js b/test/_init_tests.js index fa8514fba6d9..2bbcf1d447e0 100644 --- a/test/_init_tests.js +++ b/test/_init_tests.js @@ -42,6 +42,8 @@ import PreactEnzyme from 'enzyme-adapter-preact-pure'; import sinon from /*OK*/ 'sinon'; import stringify from 'json-stable-stringify'; +chai.use(require('chai-as-promised')); + // Used to print warnings for unexpected console errors. let that; let consoleErrorSandbox; diff --git a/test/chai-as-promised/.eslintrc b/test/chai-as-promised/.eslintrc deleted file mode 100644 index 0774a2be8c6a..000000000000 --- a/test/chai-as-promised/.eslintrc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "globals": { - "require": false - } -} diff --git a/test/chai-as-promised/chai-as-promised.js b/test/chai-as-promised/chai-as-promised.js deleted file mode 100644 index 378b6c1f36c2..000000000000 --- a/test/chai-as-promised/chai-as-promised.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2017 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - * NOTE: This special file is required for three reasons: - * - * 1. The AMP unit tests use the 'chai' v4 assertion library, which depends on - * 'chai-as-promised' v7, a library that contains ES6 code. - * See https://github.com/domenic/chai-as-promised/issues/133. - * - * 2. We cannot use 'chai-as-promised' on Sauce labs, since some browsers only - * support ES5 code. - * See https://github.com/domenic/chai-as-promised#browsernode-compatibility. - * - * 3. Transpiling 'chai-as-promised' with babelify is not possible - * since directories under `node_modules/` are ignored by default. - * See https://github.com/babel/babelify/issues/53. - * - * We therefore include this file while running unit tests (only run on chrome), - * but not while running integration tests (run on multiple browsers). - * As a result, integration tests may not use `chai-as-promised. - */ - -chai.use(require('chai-as-promised'));