import_tests.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* global chai */
  2. // Allow for mocking of Angular
  3. import 'angular'
  4. import 'angular-mocks'
  5. /**
  6. * Add chai assertion for comparing CodeMirror Pos objects.
  7. * A deep comparison will fail because CodeMirror inserts additional properties
  8. * that we want to ignore.
  9. */
  10. chai.Assertion.addMethod('equalPos', function (expectedPos) {
  11. const { line: actualLine, ch: actualCh } = this._obj
  12. const { line: expectedLine, ch: expectedCh } = expectedPos
  13. this.assert(
  14. actualLine === expectedLine && actualCh === expectedCh,
  15. `expected #{exp} to equal #{act}`,
  16. `expected #{exp} to not equal #{act}`,
  17. `Pos({ line: ${expectedLine}, ch: ${expectedCh} })`,
  18. `Pos({ line: ${actualLine}, ch: ${actualCh} })`
  19. )
  20. })
  21. // Mock ExposedSettings
  22. window.ExposedSettings = {}
  23. /*
  24. * Bundle all test files together into a single bundle, and run tests against
  25. * this single bundle.
  26. * We are using karma-webpack to bundle our tests and the 'default' strategy is
  27. * to create a bundle for each test file. This isolates the tests better, but
  28. * causes a problem with Angular. The issue with Angular tests is because we
  29. * load a single global copy of Angular (see karma.conf.js) but
  30. * frontend/js/base.js is included in each bundle, meaning the Angular app is
  31. * initialised for each bundle when it is loaded onto the page when Karma
  32. * starts. This means that only the last bundle will have controllers/directives
  33. * registered against it, ultimately meaning that all other bundles will fail
  34. * because Angular cannot find the controller/directive under test.
  35. */
  36. // Import from the top-level any JS files within a test/karma
  37. // directory
  38. const context = require.context('../../', true, /test\/karma\/.*\.js$/)
  39. context.keys().forEach(context)