Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed Oct 1, 2021
0 parents commit f57ec72
Show file tree
Hide file tree
Showing 14 changed files with 8,632 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
old-code/
dev/
node_modules
45 changes: 45 additions & 0 deletions .eslintrc.js
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 }]
}
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.vscode
examples
.eslintignore
.eslintrc.js
.prettierignore
.prettierrc
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
old-code/
dev/
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "none"
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
52 changes: 52 additions & 0 deletions README.md
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>
2 changes: 2 additions & 0 deletions examples/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const fs = require('../../lib');
module.exports = fs;
10 changes: 10 additions & 0 deletions examples/test1.test.js
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');
});
5 changes: 5 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs');

module.exports = {
getMockFs: fs.__getMockFs
};
36 changes: 36 additions & 0 deletions lib/index.js
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;
Loading

0 comments on commit f57ec72

Please sign in to comment.