From 659a3ab87e34e2fa7db2e67b8cf617767f4a6a9d Mon Sep 17 00:00:00 2001 From: Mohamed Elkholy Date: Mon, 15 Feb 2021 03:53:40 +0200 Subject: [PATCH] test(plugin): add some utils for testing --- package.json | 3 +- pnpm-lock.yaml | 9 ++++++ test/__mocks__/fs.js | 5 ++++ test/utils.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/__mocks__/fs.js create mode 100644 test/utils.js diff --git a/package.json b/package.json index 528e04d..0ebb3e9 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "parse5": "^6.0.0", "parse5-htmlparser2-tree-adapter": "^6.0.0", "purgecss": "^4.0.0", - "filesize": "^6.1.0" + "filesize": "^6.1.0", + "mkdirp": "^1.0.4" }, "devDependencies": { "@babel/cli": "7.12.16", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 16a3893..cf35c3c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,7 @@ dependencies: debug: 3.2.7 filesize: 6.1.0 lodash: 4.17.20 + mkdirp: 1.0.4 parse5: 6.0.1 parse5-htmlparser2-tree-adapter: 6.0.1 purgecss: 4.0.0 @@ -5371,6 +5372,13 @@ packages: hasBin: true resolution: integrity: sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ== + /mkdirp/1.0.4: + dev: false + engines: + node: '>=10' + hasBin: true + resolution: + integrity: sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== /modify-values/1.0.1: dev: true engines: @@ -7747,6 +7755,7 @@ specifiers: jest: 26.6.3 lodash: ^4.17.20 metro-memory-fs: 0.65.0 + mkdirp: ^1.0.4 parse5: ^6.0.0 parse5-htmlparser2-tree-adapter: ^6.0.0 purgecss: ^4.0.0 diff --git a/test/__mocks__/fs.js b/test/__mocks__/fs.js new file mode 100644 index 0000000..016d839 --- /dev/null +++ b/test/__mocks__/fs.js @@ -0,0 +1,5 @@ +import MemoryFS from 'metro-memory-fs' +// Make sure we export this as a cjs module +module.exports = new MemoryFS({ + cwd: () => '/virtual/project' +}) diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..3b0f437 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,70 @@ +import fs from 'fs' +import path from 'path' +import mkdirp from 'mkdirp' +import { onPreBootstrap } from '../src' + +/** + * Make sure to mock the fs module in your test + * file before using any of these functions + */ + +/** + * Virtual project root directory + * @see /test/__mocks__/fs.js + * @type {string} + */ +export const programRoot = '/virtual/project' + +/** + * Changes plugin options by invoking the onPreBootstrap hook + * Tests that run after calling this function will receive + * the mounted options + * + * Note: Make sure to wrap this in a try..catch block when you + * pass custom options since it may throw errors + * + * @param {Object} options + */ +export function mountOptions (options = {}) { + onPreBootstrap({ + store: { + getState: () => ({ + program: { directory: programRoot } + }) + }, + reporter: {}, + pathPrefix: '' + }, options) +} + +/** + * Mounts a directory on the virtual filesystem + * + * @param {string} dirname + */ +export function mountDir (dirname) { + dirname = path.resolve(programRoot, dirname) + !fs.existsSync(dirname) ? mkdirp.sync(dirname) : null +} + +/** + * Mounts a file on the virtual filesystem + * + * @param {string} filename + * @param {string} content + */ +export function mountFile (filename, content = '//noop') { + mountDir(path.dirname(filename)) + fs.writeFileSync(path.resolve(programRoot, filename), content.toString()) +} + +/** + * Mocks a virtual module by path Usefull when requiring + * a module that doesn't exist on the filesystem + * + * @param {string} name - module name + * @param {object} exports - module exports + */ +export function mountModule (name, exports) { + jest.doMock(path.resolve(programRoot, name), () => exports, { virtual: true }) +}