-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feature request: add chai-as-promised to dev-dependency #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was able to get this to work, but it took some time sifting through all of them comments and incompatibilities. Install: To use in my test added at the top: const expect = require('chai').use(require('chai-as-promised')).expect; And in my test case: expect(promisefunc()).to.eventually.equal(123); karma.conf.js:
complete file: var webpackConfig = require('../../build/webpack.test.conf');
module.exports = function (config) {
config.set({
browsers: ['PhantomJS'],
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'],
reporters: ['spec', 'coverage'],
files: ['../../node_modules/babel-polyfill/dist/polyfill.js',
'./index.js'],
preprocessors: {
'./index.js': ['webpack', 'sourcemap'],
[require.resolve('chai-as-promised')]: ['webpack'],
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true,
},
coverageReporter: {
dir: './coverage',
reporters: [
{ type: 'lcov', subdir: '.' },
{ type: 'text-summary' },
]
},
});
}; webpack.test.conf.js
complete file: // This is the webpack config used for unit tests.
var utils = require('./utils')
var webpack = require('webpack')
var merge = require('webpack-merge')
var baseConfig = require('./webpack.base.conf')
var webpackConfig = merge(baseConfig, {
// use inline sourcemap for karma-sourcemap-loader
module: {
rules: utils.styleLoaders().concat([
{
test: require.resolve('chai-as-promised'),
use: 'babel-loader'
}
])
},
devtool: '#inline-source-map',
resolveLoader: {
alias: {
// necessary to to make lang="scss" work in test when using vue-loader's ?inject option
// see discussion at https://github.com/vuejs/vue-loader/issues/724
'scss-loader': 'sass-loader'
}
},
plugins: [
new webpack.DefinePlugin({
'process.env': require('../config/test.env')
})
]
})
// no need for app entry during tests
delete webpackConfig.entry
module.exports = webpackConfig |
@aldencolerain const expect = require('chai').use(require('chai-as-promised')).expect;
describe('Test', () => {
it('test', () => {
//should failed
expect(Promise.resolve(1111)).to.eventually.equal(123);
})
}) When I run |
@DRL9 I'm not exactly sure why your example isn't working, I eventually found that using async/await was significantly easier and less difficult than using chai-as-expected. I just created a utility function to handle checking rejected promise errors. Here are some examples from my code: it('should return an empty object if we got a good response with no payload', async function() {
expect(await request('GET', '/')).to.deep.equal({});
});
it('should throw api request error with empty fields and alert user on a network error', async function() {
const error = await rejection(() => request('GET', '/'));
expect(error).to.be.instanceof(Error);
expect(error.message).to.equal('API Request Error');
}); Here is my utility function for checking rejected promises:
|
Since we will shortly switch to Jest (#817), this is soon obsolete. |
Hello!
I think many of us or totally every of us use promises in their code. So karma-chai-as-promised configured would be very good.
the main use case with chai-as-promised is testing async methods which should rejected
thanks!
The text was updated successfully, but these errors were encountered: