Skip to content

Commit

Permalink
Resolve a promise when returned in config
Browse files Browse the repository at this point in the history
This unlocks doing some async work as part of the configuration. For
example using our browserlist query string and the browserstack API to
generate a list of launchers so we're always testing against our
supporter browser matrix.
  • Loading branch information
jrjohnson committed Aug 29, 2022
1 parent 79b3fa4 commit e947a14
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
20 changes: 20 additions & 0 deletions docs/config_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ Hooks can be defined as a string in which case they run as a shell command or as
before_tests: Runs before every run of tests
after_tests: Runs after every run of tests
on_exit: Runs before suite exits

### Returning a Promise from `testem.js`

If you need to resolve some async values in your configuration you can return a promise from `testem.js`. The resolved value of
the promise will be passed to testem as configuration.

```javascript
'use strict';

async function getFramework() {
//do something async here, like generating a list of launchers based on configuration
return 'qunit';
}

module.exports = async function() {
return {
framework: await getFramework(),
}
};
```
16 changes: 13 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,19 @@ class Config {
}

readJS(configFile, callback) {
this.fileOptions = require(this.resolveConfigPath(configFile));
if (callback) {
callback.call(this);
let exportedConfig = require(this.resolveConfigPath(configFile));
if (typeof exportedConfig === 'function') {
Promise.resolve(exportedConfig()).then(obj => {
this.fileOptions = obj;
if (callback) {
callback.call(this);
}
});
} else {
this.fileOptions = exportedConfig;
if (callback) {
callback.call(this);
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/config_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ describe('Config', function() {
});
});

describe('resolve promise from js config', function() {
let config;
beforeEach(function(done) {
let progOptions = {
file: __dirname + '/custom_configs/testem-promise.js'
};
config = new Config('dev', progOptions);
config.read(done);
});
it('gets properties from config file', function() {
expect(config.get('framework')).to.equal('qunit');
});
});

describe('getters system', function() {
it('gives precendence to getters', function(done) {
let config = new Config('dev', {cwd: 'tests'});
Expand Down
9 changes: 9 additions & 0 deletions tests/custom_configs/testem-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function() {
return new Promise(resolve => {
resolve({
framework: 'qunit',
});
});
};

0 comments on commit e947a14

Please sign in to comment.