Merge pull request #2 from medayo/shimTests

Shim tests and buffer shim/docs
This commit is contained in:
Ben Heidemann 2021-03-07 15:23:45 +00:00 committed by GitHub
commit ce076729f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 148 additions and 3 deletions

View File

@ -151,6 +151,51 @@ shim will ensure that calls to path are appropriately handled by filer.
import path from 'path';
```
It may be necessary in certain cases to shim the node.js [Buffer object](http://nodejs.org/api/buffer.html). This can be impoerted as follows:
```javascript
import path from 'path';
```
As such it can be shimmed in much the same way as path and fs:
```javascript
// webpack.config.js
module.exports = {
resolve: {
alias: {
'buffer': 'filer/shims/buffer.js',
}
}
}
```
However, the Buffer object is globally defined in a node environment and many third party libraries will not import (or require) it.
Using the resolve alias alone will be ineffective in such cases. Instead we must expand our webpack config to use webpacks
[provide plugin](https://webpack.js.org/plugins/provide-plugin/), which will automatically load the module without the need for an import
or require. This can be implemented as follows:
```javascript
// webpack.config.js
const webpack = require('webpack');
module.exports = {
resolve: {
alias: {
'buffer': 'filer/shims/buffer.js',
}
},
plugins: [
new webpack.ProvidePlugin({
Buffer: 'buffer',
}),
]
}
```
This will, in effect, make the Buffer object shim globally available in the same way that the node.js
[Buffer object](http://nodejs.org/api/buffer.html) is in a node environment.
### Getting Started
Filer is as close to the node.js [fs module](http://nodejs.org/api/fs.html) as possible,

View File

@ -2,10 +2,14 @@ module.exports = function(config) {
config.set({
singleRun: true,
basePath: '',
files: ['tests/dist/index.js'],
files: [
'node_modules/regenerator-runtime/runtime.js',
'tests/dist/index.js'
],
frameworks: ['mocha', 'chai'],
reporters: ['mocha', 'summary'],
client: {
captureConsole: true,
mocha: {
ui: 'bdd',
timeout: 5000,

View File

@ -49,6 +49,7 @@
"minimatch": "^3.0.4"
},
"devDependencies": {
"regenerator-runtime": "^0.13.7",
"chai": "^4.2.0",
"chai-datetime": "^1.5.0",
"eslint": "^6.8.0",

7
shims/buffer.js Normal file
View File

@ -0,0 +1,7 @@
const { Buffer } = require('../src/index');
module.exports = {
__esModule: true,
default: Buffer,
Buffer,
};

View File

@ -1,4 +1,4 @@
const path = require("./path.js");
const { path } = require('../src/index');
module.exports = {
__esModule: true,

View File

@ -3,6 +3,11 @@
* get them running by default.
*/
// Shims
require('./spec/shims/fs.spec');
require('./spec/shims/path.spec');
require('./spec/shims/buffer.spec');
// Filer
require('./spec/filer.spec');
require('./spec/filer.buffer.spec.js');

View File

@ -52,11 +52,13 @@ function shimIndexedDB(fn) {
global.indexedDB = require('fake-indexeddb');
}
fn();
var result = fn();
if(addShim) {
delete global.indexedDB;
}
return result;
}
function setup(callback) {

View File

@ -0,0 +1,23 @@
'use strict';
const expect = require('chai').expect;
const bufferDefault = require('../../../shims/buffer').default;
const bufferNamed = require('../../../shims/buffer').Buffer;
const bufferActual = require('../../../src/index').Buffer;
describe.only('path shim', () => {
it('default export should be defined', () => {
expect(bufferDefault).to.not.be.undefined;
});
it('named export should be defined', () => {
expect(bufferNamed).to.not.be.undefined;
});
it('default export should be re-exposing Buffer', () => {
expect(bufferDefault).to.equal(bufferActual);
});
it('named export should be re-exposing Buffer', () => {
expect(bufferNamed).to.equal(bufferActual);
});
});

View File

@ -0,0 +1,44 @@
'use strict';
const expect = require('chai').expect;
const utils = require('../../lib/test-utils');
const fs = utils.shimIndexedDB(() => require('../../../shims/fs').default);
describe.only('fs shim', () => {
it('should be defined', () => {
expect(fs).to.not.be.undefined;
});
it('should be an object', () => {
expect(typeof fs).to.equal('object');
});
it('should return a function when accessing fs.writeFile', () => {
expect(typeof fs.writeFile).to.equal('function');
});
it('should call callback when calling fs.writeFile', (done) => {
fs.writeFile('/test.txt', 'test', function(err) {
if(err) throw err;
done();
});
});
it('should return an object when accessing fs.promises', () => {
expect(typeof fs.promises).to.equal('object');
});
it('should return a function when accessing fs.promises.writeFile', () => {
expect(typeof fs.promises.writeFile).to.equal('function');
});
it('should return a promise which resolves when calling fs.promises.writeFile', (done) => {
const writeFilePromise = fs.promises.writeFile('/test2.txt', '');
expect(writeFilePromise instanceof Promise).to.equal(true);
writeFilePromise.then(() => {
done();
}).catch((err) => {
done(err);
});
});
});

View File

@ -0,0 +1,14 @@
'use strict';
const expect = require('chai').expect;
const path = require('../../../shims/path').default;
const pathActual = require('../../../src/index').path;
describe.only('path shim', () => {
it('should be defined', () => {
expect(path).to.not.be.undefined;
});
it('should be re-exposing path', () => {
expect(path).to.equal(pathActual);
});
});