Merge branch 'winh-jest-html-fixtures' into 'master'
Add helpers for HTML fixtures to Jest Closes #57998 See merge request gitlab-org/gitlab-ce!26739
This commit is contained in:
commit
b4cc639f80
|
@ -6,4 +6,9 @@ plugins:
|
|||
settings:
|
||||
import/resolver:
|
||||
jest:
|
||||
jestConfigFile: "jest.config.js"
|
||||
jestConfigFile: 'jest.config.js'
|
||||
globals:
|
||||
getJSONFixture: false
|
||||
loadFixtures: false
|
||||
preloadFixtures: false
|
||||
setFixtures: false
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
/* eslint-disable import/prefer-default-export, global-require, import/no-dynamic-require */
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
|
@ -7,16 +5,32 @@ 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);
|
||||
export function getFixture(relativePath) {
|
||||
const absolutePath = path.join(fixturesBasePath, relativePath);
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new ErrorWithStack(
|
||||
`Fixture file ${relativePath} does not exist.
|
||||
|
||||
Did you run bin/rake karma:fixtures?`,
|
||||
getJSONFixture,
|
||||
getFixture,
|
||||
);
|
||||
}
|
||||
|
||||
return require(absolutePath);
|
||||
return fs.readFileSync(absolutePath, 'utf8');
|
||||
}
|
||||
|
||||
export const getJSONFixture = relativePath => JSON.parse(getFixture(relativePath));
|
||||
|
||||
export const resetHTMLFixture = () => {
|
||||
document.body.textContent = '';
|
||||
};
|
||||
|
||||
export const setHTMLFixture = (htmlContent, resetHook = afterEach) => {
|
||||
document.body.outerHTML = htmlContent;
|
||||
resetHook(resetHTMLFixture);
|
||||
};
|
||||
|
||||
export const loadHTMLFixture = (relativePath, resetHook = afterEach) => {
|
||||
const fileContent = getFixture(relativePath);
|
||||
setHTMLFixture(fileContent, resetHook);
|
||||
};
|
||||
|
|
|
@ -16,9 +16,9 @@ describe('Abuse Reports', () => {
|
|||
|
||||
preloadFixtures(FIXTURE);
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(() => {
|
||||
loadFixtures(FIXTURE);
|
||||
this.abuseReports = new AbuseReports();
|
||||
new AbuseReports(); // eslint-disable-line no-new
|
||||
$messages = $('.abuse-reports .message');
|
||||
});
|
||||
|
|
@ -2,6 +2,7 @@ import Vue from 'vue';
|
|||
import Translate from '~/vue_shared/translate';
|
||||
import axios from '~/lib/utils/axios_utils';
|
||||
import { initializeTestTimeout } from './helpers/timeout';
|
||||
import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './helpers/fixtures';
|
||||
|
||||
// wait for pending setTimeout()s
|
||||
afterEach(() => {
|
||||
|
@ -26,3 +27,20 @@ Vue.config.devtools = false;
|
|||
Vue.config.productionTip = false;
|
||||
|
||||
Vue.use(Translate);
|
||||
|
||||
// workaround for JSDOM not supporting innerText
|
||||
// see https://github.com/jsdom/jsdom/issues/1245
|
||||
Object.defineProperty(global.Element.prototype, 'innerText', {
|
||||
get() {
|
||||
return this.textContent;
|
||||
},
|
||||
configurable: true, // make it so that it doesn't blow chunks on re-running tests with things like --watch
|
||||
});
|
||||
|
||||
// convenience wrapper for migration from Karma
|
||||
Object.assign(global, {
|
||||
loadFixtures: loadHTMLFixture,
|
||||
loadJSONFixtures: getJSONFixture,
|
||||
preloadFixtures() {},
|
||||
setFixtures: setHTMLFixture,
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue