From af3307f7b4fdf2a02f94e246f84e336c7b89f3a0 Mon Sep 17 00:00:00 2001 From: Winnie Hellmann Date: Mon, 11 Mar 2019 14:47:36 +0100 Subject: Add getJSONFixture() helper to Jest --- spec/frontend/helpers/fixtures.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 spec/frontend/helpers/fixtures.js (limited to 'spec/frontend/helpers') 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); +} -- cgit v1.2.1