-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setupTests.js
41 lines (34 loc) · 1.26 KB
/
jest.setupTests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } = require('fs');
const crypto = require('crypto');
const { extname, join, resolve } = require('path');
global.__basedir = __dirname;
/**
* Generate a fixture from string literals.
*
* @param {string} contents
* @param {object} options
* @param {string} options.dir
* @param {string} options.ext
* @param {string} options.encoding
* @param {string} options.filename
* @param {boolean} options.resetDir
* @returns {{path: string, file: string, contents: *, dir: string}}
*/
const generateFixture = (
contents,
{ dir = resolve(__dirname, '.fixtures'), ext = 'txt', encoding = 'utf8', filename, resetDir = true } = {}
) => {
const updatedFileName = filename || crypto.createHash('md5').update(contents).digest('hex');
const file = extname(updatedFileName) ? updatedFileName : `${updatedFileName}.${ext}`;
const path = join(dir, file);
if (resetDir && existsSync(dir)) {
rmSync(dir, { recursive: true });
}
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(path, contents, { encoding });
const updatedContents = readFileSync(path, { encoding });
return { dir, file, path, contents: updatedContents };
};
global.generateFixture = generateFixture;