summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWinnie Hellmann <winnie@gitlab.com>2019-03-11 14:47:36 +0100
committerWinnie Hellmann <winnie@gitlab.com>2019-03-11 14:55:18 +0100
commitaf3307f7b4fdf2a02f94e246f84e336c7b89f3a0 (patch)
tree2cd89b385b685aef3d9f06f77a2231c34ba55774
parentaeffdd2acd4e8083c2b65e8908969c4ed03541df (diff)
downloadgitlab-ce-af3307f7b4fdf2a02f94e246f84e336c7b89f3a0.tar.gz
Add getJSONFixture() helper to Jest
-rw-r--r--jest.config.js1
-rw-r--r--spec/frontend/gfm_auto_complete_spec.js5
-rw-r--r--spec/frontend/helpers/fixtures.js24
3 files changed, 27 insertions, 3 deletions
diff --git a/jest.config.js b/jest.config.js
index 4e346005b8a..efbf2e602c1 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -18,7 +18,6 @@ module.exports = {
moduleNameMapper: {
'^~(.*)$': '<rootDir>/app/assets/javascripts$1',
'^ee(.*)$': '<rootDir>/ee/app/assets/javascripts$1',
- '^fixtures(.*)$': '<rootDir>/spec/javascripts/fixtures$1',
'^helpers(.*)$': '<rootDir>/spec/frontend/helpers$1',
'^vendor(.*)$': '<rootDir>/vendor/assets/javascripts$1',
'\\.(jpg|jpeg|png|svg)$': '<rootDir>/spec/frontend/__mocks__/file_mock.js',
diff --git a/spec/frontend/gfm_auto_complete_spec.js b/spec/frontend/gfm_auto_complete_spec.js
index 9c0b8d676e0..675bc660755 100644
--- a/spec/frontend/gfm_auto_complete_spec.js
+++ b/spec/frontend/gfm_auto_complete_spec.js
@@ -8,11 +8,12 @@ import 'vendor/jquery.atwho';
import { TEST_HOST } from 'helpers/test_constants';
import { setTestTimeout } from 'helpers/timeout';
-
-import labelsFixture from 'fixtures/autocomplete_sources/labels.json'; // eslint-disable-line import/no-unresolved
+import { getJSONFixture } from 'helpers/fixtures';
setTestTimeout(500);
+const labelsFixture = getJSONFixture('autocomplete_sources/labels.json');
+
describe('GfmAutoComplete', () => {
const gfmAutoCompleteCallbacks = GfmAutoComplete.prototype.getDefaultCallbacks.call({
fetchData: () => {},
diff --git a/spec/frontend/helpers/fixtures.js b/spec/frontend/helpers/fixtures.js
new file mode 100644
index 00000000000..f96f27c4d80
--- /dev/null
+++ b/spec/frontend/helpers/fixtures.js
@@ -0,0 +1,24 @@
+/* eslint-disable import/prefer-default-export, global-require, import/no-dynamic-require */
+
+import fs from 'fs';
+import path from 'path';
+
+// jest-util is part of Jest
+// eslint-disable-next-line import/no-extraneous-dependencies
+import { ErrorWithStack } from 'jest-util';
+
+const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures');
+
+export function getJSONFixture(relativePath, ee = false) {
+ const absolutePath = path.join(fixturesBasePath, ee ? 'ee' : '', relativePath);
+ if (!fs.existsSync(absolutePath)) {
+ throw new ErrorWithStack(
+ `Fixture file ${relativePath} does not exist.
+
+Did you run bin/rake karma:fixtures?`,
+ getJSONFixture,
+ );
+ }
+
+ return require(absolutePath);
+}