run_tests.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright 2018 Google Inc. All Rights Reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. const rollup = require("rollup");
  14. const path = require("path");
  15. const omt = require(".");
  16. const fs = require("fs");
  17. const karma = require("karma");
  18. const myKarmaConfig = require("./karma.conf.js");
  19. async function fileExists(file) {
  20. try {
  21. const stat = await fs.promises.stat(file);
  22. return stat.isFile();
  23. } catch (e) {
  24. return false;
  25. }
  26. }
  27. async function init() {
  28. await Promise.all(
  29. [
  30. "./tests/fixtures/simple-bundle/entry.js",
  31. "./tests/fixtures/import-meta/entry.js",
  32. "./tests/fixtures/dynamic-import/entry.js",
  33. "./tests/fixtures/public-path/entry.js",
  34. "./tests/fixtures/worker/entry.js",
  35. "./tests/fixtures/module-worker/entry.js",
  36. "./tests/fixtures/more-workers/entry.js",
  37. "./tests/fixtures/amd-function-name/entry.js",
  38. "./tests/fixtures/single-default/entry.js",
  39. "./tests/fixtures/import-worker-url/entry.js",
  40. "./tests/fixtures/import-worker-url-custom-scheme/entry.js",
  41. "./tests/fixtures/assets-in-worker/entry.js"
  42. ].map(async input => {
  43. const pathName = path.dirname(input);
  44. const outputOptions = {
  45. dir: path.join(pathName, "build"),
  46. format: "amd"
  47. };
  48. let rollupConfig = {
  49. input,
  50. strictDeprecations: true
  51. };
  52. const rollupConfigPath = "./" + path.join(pathName, "rollup.config.js");
  53. const configPath = "./" + path.join(pathName, "config.json");
  54. if (await fileExists(rollupConfigPath)) {
  55. require(rollupConfigPath)(rollupConfig, outputOptions, omt);
  56. } else if (await fileExists(configPath)) {
  57. rollupConfig.plugins = [omt(require(configPath))];
  58. } else {
  59. rollupConfig.plugins = [omt()];
  60. }
  61. const bundle = await rollup.rollup(rollupConfig);
  62. await bundle.write(outputOptions);
  63. })
  64. );
  65. const karmaConfig = { port: 9876 };
  66. myKarmaConfig({
  67. set(config) {
  68. Object.assign(karmaConfig, config);
  69. }
  70. });
  71. const server = new karma.Server(karmaConfig, code => {
  72. console.log(`Karma exited with code ${code}`);
  73. process.exit(code);
  74. });
  75. server.start();
  76. }
  77. init();