-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(plugin): add some utils for testing
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
} |