Skip to content

Commit b007e00

Browse files
feat(test): new fixture loading mechanism
1 parent 007ee01 commit b007e00

File tree

7 files changed

+132
-33
lines changed

7 files changed

+132
-33
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,53 @@ You will need
8484
- `SAUCE_USERNAME=<username>`
8585
- `SAUCE_ACCESS_KEY=<access key>`
8686

87+
#### Fixtures
88+
89+
Loading fixture files in node and the browser can be painful, that's why aegir provides
90+
a method to do this. For it to work you have to put your fixtures in the folder `test/fixtures`, and then
91+
92+
```js
93+
// test/awesome.spec.js
94+
const loadFixture = require('aegir/fixtures')
95+
96+
const myFixture = loadFixture(__dirname, 'fixtures/largefixture')
97+
```
98+
99+
If you write a module like [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core)
100+
which is to be consumed by other modules tests you need to pass in a third parameter such that
101+
the server is able to serve the correct files.
102+
103+
For example
104+
105+
```js
106+
// awesome-tests module
107+
const loadFixture = require('aegir/fixtures'
108+
109+
const myFixture = loadFixture(__dirname, 'fixtures/coolfixture', 'awesome-tests')
110+
```
111+
112+
```js
113+
// tests for module using the awesome-tests
114+
require('awesome-tests')
115+
```
116+
117+
```js
118+
// .aegir.js file in the module using the awesome-tests module
119+
'use strict'
120+
121+
module.exports = {
122+
karma: {
123+
files: [{
124+
pattern: 'node_modules/awesome-tests/test/fixtures/**/*',
125+
watched: false,
126+
served: true,
127+
included: false
128+
}]
129+
}
130+
}
131+
```
132+
133+
87134
### Coverage
88135
89136
You can run it using

config/karma.conf.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
const webpackConfig = require('./webpack')
44
const timeout = require('./custom').timeout
5+
const user = require('./user').customConfig
6+
7+
let userFiles = []
8+
if (user.karma && user.karma.files) {
9+
userFiles = user.karma.files
10+
}
11+
12+
const files = [
13+
'test/browser.js',
14+
'test/**/*.spec.js',
15+
{pattern: 'test/fixtures/**/*', watched: false, served: true, included: false}
16+
].concat(userFiles)
517

618
let concurrency = 1
719
let reporters = ['mocha-own']
@@ -78,17 +90,13 @@ module.exports = function (config) {
7890
timeout: timeout
7991
}
8092
},
81-
files: [
82-
'test/browser.js',
83-
'test/**/*.spec.js'
84-
],
85-
exclude: [],
93+
files: files,
8694
preprocessors: {
87-
'test/**/*': ['webpack', 'sourcemap']
95+
'test/**/*.js': ['webpack', 'sourcemap']
8896
},
8997
webpack: webpackConfig,
9098
webpackMiddleware: {
91-
noInfo: true
99+
noInfo: false
92100
},
93101
reporters: reporters,
94102
mochaOwnReporter: {

config/user.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const pkg = require(path.resolve('package.json'))
5+
6+
let customConfig = {}
7+
try {
8+
customConfig = require(path.resolve('.aegir.js'))
9+
} catch (err) {
10+
}
11+
12+
module.exports = {
13+
pkg: pkg,
14+
customPkg: pkg.aegir || {},
15+
customConfig: customConfig || {}
16+
}

config/webpack.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@ const upperFirst = require('lodash.upperfirst')
55
const camelCase = require('lodash.camelcase')
66
const merge = require('webpack-merge')
77

8-
const pkg = require(path.resolve('package.json'))
9-
let customConfig = {}
10-
try {
11-
customConfig = require(path.resolve('.aegir.js'))
12-
} catch (err) {
13-
}
8+
let user = require('./user')
149

1510
// e.g. peer-id -> PeerId
16-
const libraryName = upperFirst(camelCase(pkg.name))
17-
18-
let custom1 = {}
19-
let custom2 = {}
20-
21-
if (pkg.aegir && pkg.aegir.webpack) {
22-
custom1 = pkg.aegir.webpack
23-
}
24-
25-
if (customConfig && customConfig.webpack) {
26-
custom2 = customConfig.webpack
27-
}
28-
29-
const specific = merge(custom1, custom2)
11+
const libraryName = upperFirst(camelCase(user.pkg.name))
12+
const specific = merge(user.customPkg || {}, user.customPkg || {})
3013

3114
const shared = {
3215
entry: [
@@ -41,7 +24,10 @@ const shared = {
4124
modules: [
4225
'node_modules',
4326
path.resolve(__dirname, '../node_modules')
44-
]
27+
],
28+
alias: {
29+
zlib: 'browserify-zlib'
30+
}
4531
},
4632
resolveLoader: {
4733
modules: [

fixtures.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const isNode = require('detect-node')
4+
const path = require('path')
5+
6+
module.exports = function loadFixtures (dirname, file, module) {
7+
if (isNode) {
8+
return require('fs').readFileSync(path.join(dirname, file))
9+
} else {
10+
return syncXhr(file, module)
11+
}
12+
}
13+
14+
// I know this is considered bad practice, but it makes testing life so much nicer!
15+
function syncXhr (url, module) {
16+
let target
17+
if (module) {
18+
target = path.join('base', 'node_modules', module, 'test', url)
19+
} else {
20+
target = path.join('base', 'test', url)
21+
}
22+
const request = new window.XMLHttpRequest()
23+
request.open('GET', target, false)
24+
request.overrideMimeType('text/plain; charset=x-user-defined')
25+
request.send(null)
26+
27+
if (request.status === 200) {
28+
const filestream = request.responseText
29+
let res = new Uint8Array(filestream.length)
30+
31+
for (let i = 0; i < filestream.length; i++) {
32+
res[i] = filestream.charCodeAt(i) & 0xff
33+
}
34+
35+
return new Buffer(res)
36+
}
37+
}

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "9.0.1",
44
"description": "JavaScript project management",
55
"main": "index.js",
6+
"browser": {
7+
"fs": false
8+
},
69
"bin": {
710
"aegir-lint": "bin/lint",
811
"aegir-build": "bin/build",
@@ -27,12 +30,13 @@
2730
"chalk": "^1.1.3",
2831
"conventional-github-releaser": "^1.1.3",
2932
"coveralls": "^2.11.14",
30-
"eslint": "^3.8.1",
33+
"detect-node": "^2.0.3",
34+
"eslint": "^3.9.1",
3135
"eslint-config-standard": "^6.2.1",
3236
"eslint-plugin-promise": "^3.3.0",
3337
"eslint-plugin-standard": "^2.0.1",
3438
"gulp": "^3.9.1",
35-
"gulp-bump": "^2.4.0",
39+
"gulp-bump": "^2.5.0",
3640
"gulp-conventional-changelog": "^1.1.0",
3741
"gulp-eslint": "^3.0.1",
3842
"gulp-filter": "^4.0.0",
@@ -65,7 +69,7 @@
6569
"run-sequence": "^1.2.2",
6670
"semver": "^5.3.0",
6771
"signal-exit": "^3.0.1",
68-
"stream-http": "^2.4.0",
72+
"stream-http": "^2.4.1",
6973
"transform-loader": "^0.2.3",
7074
"uglify-js": "github:mishoo/UglifyJS2#harmony",
7175
"webpack": "^2.1.0-beta.25",
@@ -86,4 +90,4 @@
8690
"greenkeeperio-bot <[email protected]>",
8791
"npmcdn-to-unpkg-bot <[email protected]>"
8892
]
89-
}
93+
}

tasks/lint.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = (gulp) => {
1111
'test/**/*.js',
1212
'src/**/*.js',
1313
'tasks/**/*.js',
14-
'examples/**/*.js'
14+
'examples/**/*.js',
15+
'benchmarks/**/*.js'
1516
])
1617
.pipe(eslint(`${__dirname}/../config/eslintrc.yml`))
1718
.pipe(eslint.format())

0 commit comments

Comments
 (0)