Merge pull request #2 from medayo/shimTests
Shim tests and buffer shim/docs
This commit is contained in:
commit
ce076729f6
45
README.md
45
README.md
|
@ -151,6 +151,51 @@ shim will ensure that calls to path are appropriately handled by filer.
|
||||||
import path from 'path';
|
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
|
### Getting Started
|
||||||
|
|
||||||
Filer is as close to the node.js [fs module](http://nodejs.org/api/fs.html) as possible,
|
Filer is as close to the node.js [fs module](http://nodejs.org/api/fs.html) as possible,
|
||||||
|
|
|
@ -2,10 +2,14 @@ module.exports = function(config) {
|
||||||
config.set({
|
config.set({
|
||||||
singleRun: true,
|
singleRun: true,
|
||||||
basePath: '',
|
basePath: '',
|
||||||
files: ['tests/dist/index.js'],
|
files: [
|
||||||
|
'node_modules/regenerator-runtime/runtime.js',
|
||||||
|
'tests/dist/index.js'
|
||||||
|
],
|
||||||
frameworks: ['mocha', 'chai'],
|
frameworks: ['mocha', 'chai'],
|
||||||
reporters: ['mocha', 'summary'],
|
reporters: ['mocha', 'summary'],
|
||||||
client: {
|
client: {
|
||||||
|
captureConsole: true,
|
||||||
mocha: {
|
mocha: {
|
||||||
ui: 'bdd',
|
ui: 'bdd',
|
||||||
timeout: 5000,
|
timeout: 5000,
|
||||||
|
|
|
@ -49,6 +49,7 @@
|
||||||
"minimatch": "^3.0.4"
|
"minimatch": "^3.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"regenerator-runtime": "^0.13.7",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"chai-datetime": "^1.5.0",
|
"chai-datetime": "^1.5.0",
|
||||||
"eslint": "^6.8.0",
|
"eslint": "^6.8.0",
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
const { Buffer } = require('../src/index');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
__esModule: true,
|
||||||
|
default: Buffer,
|
||||||
|
Buffer,
|
||||||
|
};
|
|
@ -1,4 +1,4 @@
|
||||||
const path = require("./path.js");
|
const { path } = require('../src/index');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
__esModule: true,
|
__esModule: true,
|
||||||
|
|
|
@ -3,6 +3,11 @@
|
||||||
* get them running by default.
|
* get them running by default.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Shims
|
||||||
|
require('./spec/shims/fs.spec');
|
||||||
|
require('./spec/shims/path.spec');
|
||||||
|
require('./spec/shims/buffer.spec');
|
||||||
|
|
||||||
// Filer
|
// Filer
|
||||||
require('./spec/filer.spec');
|
require('./spec/filer.spec');
|
||||||
require('./spec/filer.buffer.spec.js');
|
require('./spec/filer.buffer.spec.js');
|
||||||
|
|
|
@ -52,11 +52,13 @@ function shimIndexedDB(fn) {
|
||||||
global.indexedDB = require('fake-indexeddb');
|
global.indexedDB = require('fake-indexeddb');
|
||||||
}
|
}
|
||||||
|
|
||||||
fn();
|
var result = fn();
|
||||||
|
|
||||||
if(addShim) {
|
if(addShim) {
|
||||||
delete global.indexedDB;
|
delete global.indexedDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup(callback) {
|
function setup(callback) {
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue