This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
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.
- Loading branch information
0 parents
commit f57ec72
Showing
14 changed files
with
8,632 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist/ | ||
old-code/ | ||
dev/ | ||
node_modules |
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,45 @@ | ||
module.exports = { | ||
env: { | ||
es6: true, | ||
node: true, | ||
jest: true | ||
}, | ||
extends: ['google', 'prettier'], | ||
plugins: ['prettier'], | ||
globals: { | ||
Atomics: 'readonly', | ||
SharedArrayBuffer: 'readonly' | ||
}, | ||
parserOptions: { | ||
ecmaVersion: 2018 | ||
}, | ||
rules: { | ||
'prettier/prettier': ['error'], | ||
'comma-dangle': 0, | ||
'require-jsdoc': 0, | ||
'arrow-parens': 0, | ||
'guard-for-in': 0, | ||
'valid-jsdoc': 0, | ||
'no-undef': 'error', | ||
'operator-linebreak': [ | ||
'error', | ||
'after', | ||
{ | ||
overrides: { '?': 'ignore', ':': 'ignore', '+': 'ignore' } | ||
} | ||
], | ||
indent: [ | ||
'error', | ||
2, | ||
{ | ||
CallExpression: { arguments: 'first' }, | ||
ignoredNodes: [ | ||
'CallExpression > CallExpression', | ||
'CallExpression > MemberExpression' | ||
], | ||
SwitchCase: 1 | ||
} | ||
], | ||
'max-len': ['error', { code: 80, ignoreComments: true }] | ||
} | ||
}; |
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 @@ | ||
node_modules |
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,7 @@ | ||
node_modules | ||
.vscode | ||
examples | ||
.eslintignore | ||
.eslintrc.js | ||
.prettierignore | ||
.prettierrc |
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,3 @@ | ||
dist/ | ||
old-code/ | ||
dev/ |
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,6 @@ | ||
{ | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 80, | ||
"trailingComma": "none" | ||
} |
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,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
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,52 @@ | ||
# jest-mock-fs | ||
|
||
Node's `fs` mock for [jest](https://jestjs.io/). | ||
|
||
## APIs Implemented | ||
- [x] `fs.readFileSync` | ||
- [x] `fs.readdirSync` | ||
- [x] `fs.writeFileSync` | ||
- [x] `fs.readFile` | ||
- [x] `fs.readdir` | ||
- [x] `fs.writeFile` | ||
- [ ] `fs.stat` | ||
- [ ] `fs.unlink` | ||
- [ ] `fs.mkdir` | ||
- [ ] `fs.rmdir` | ||
|
||
## Setup | ||
|
||
**Step 1:** Install library | ||
```sh | ||
npm i --save-dev jest-mock-fs | ||
``` | ||
|
||
**Step 2:** Create a mock and re-export library | ||
Inside your `tests/__mocks__/fs.js` | ||
```js | ||
const fs = require('jest-mock-fs'); | ||
module.exports = fs; | ||
``` | ||
|
||
**Step 3:** Add mock | ||
Inside your `tests/example.test.js` | ||
```js | ||
jest.mock('fs'); | ||
``` | ||
|
||
<details> | ||
<summary>View Full <code>example.test.js</code> file</summary> | ||
|
||
```js | ||
const fs = require('fs'); | ||
jest.mock('fs'); | ||
|
||
test('should return content', async () => { | ||
// This will not create an actual file in your file system | ||
fs.writeFileSync('test.txt', 'test'); | ||
const content = fs.readFileSync('test.txt', 'utf-8'); | ||
expect(content).toBe('test'); | ||
}); | ||
|
||
``` | ||
</details> |
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,2 @@ | ||
const fs = require('../../lib'); | ||
module.exports = fs; |
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,10 @@ | ||
const fs = require('fs'); | ||
jest.mock('fs'); | ||
const { getMockFs } = require('../lib/helpers'); | ||
|
||
test('should pass', async () => { | ||
fs.writeFileSync('test.txt', 'test'); | ||
const content = fs.readFileSync('test.txt'); | ||
console.log(getMockFs()); | ||
expect(content).toBe('test'); | ||
}); |
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 @@ | ||
const fs = require('fs'); | ||
|
||
module.exports = { | ||
getMockFs: fs.__getMockFs | ||
}; |
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,36 @@ | ||
const fs = jest.createMockFromModule('fs'); | ||
|
||
const mockFs = {}; | ||
|
||
fs.readFile = async (...args) => fs.readFileSync(...args); | ||
|
||
fs.writeFile = async (...args) => { | ||
fs.writeFileSync(...args); | ||
return Promise.resolve(); | ||
}; | ||
|
||
fs.readFileSync = (filePath, encoding) => { | ||
const content = mockFs[filePath]; | ||
if (typeof encoding === 'string' && encoding.includes('utf')) { | ||
return content; | ||
} | ||
|
||
return Buffer.from(content, 'utf8'); | ||
}; | ||
|
||
fs.writeFileSync = (filePath, content) => { | ||
mockFs[filePath] = content; | ||
}; | ||
|
||
fs.existsSync = (filePath) => !!mockFs[filePath]; | ||
|
||
fs.readdirSync = (dirPath) => { | ||
const files = Object.keys(mockFs).filter((filePath) => | ||
filePath.startsWith(dirPath) | ||
); | ||
return files; | ||
}; | ||
|
||
fs.__getMockFs = () => mockFs; | ||
|
||
module.exports = fs; |
Oops, something went wrong.