test: change karma to jest

pull/22/head
tjz 2018-05-13 23:11:51 +08:00
parent d86e333491
commit 059b928b09
20 changed files with 677 additions and 440 deletions

15
.babelrc Normal file
View File

@ -0,0 +1,15 @@
{
"env": {
"test": {
"presets": [
["env", { "targets": { "node": "current" } }]
],
"plugins": [
"transform-vue-jsx",
"transform-object-assign",
"transform-object-rest-spread",
"transform-class-properties"
]
}
}
}

View File

@ -1,4 +1,11 @@
{ {
"env": {
"browser": true,
"node": true,
"jasmine": true,
"jest": true,
"es6": true
},
"parser": "babel-eslint", "parser": "babel-eslint",
"extends": ["plugin:vue-libs/recommended"], "extends": ["plugin:vue-libs/recommended"],
"rules": { "rules": {

2
.gitignore vendored
View File

@ -65,7 +65,7 @@ es
site-dist site-dist
yarn.lock yarn.lock
package-lock.json package-lock.json
/test/coverage /coverage
# 备份文件 # 备份文件
/components/test/* /components/test/*

48
.jest.js Normal file
View File

@ -0,0 +1,48 @@
const libDir = process.env.LIB_DIR;
const transformIgnorePatterns = [
'/dist/',
'node_modules\/[^/]+?\/(?!(es|node_modules)\/)', // Ignore modules without es dir
];
module.exports = {
setupFiles: [
'./tests/setup.js',
],
moduleFileExtensions: [
"js",
"jsx",
"json",
"vue",
"md"
],
modulePathIgnorePatterns: [
'/_site/',
],
testPathIgnorePatterns: [
'/node_modules/',
'node',
],
transform: {
".*\\.(vue|md)$": "<rootDir>/node_modules/vue-jest",
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest"
},
testRegex: libDir === 'dist' ? 'demo\\.test\\.js$' : '.*\\.test\\.js$',
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/$1",
"vue-antd-ui": "<rootDir>/components/index.js",
},
snapshotSerializers: [
"<rootDir>/node_modules/jest-serializer-vue"
],
collectCoverage: process.env.COVERAGE === 'true',
collectCoverageFrom: [
"components/**/*.{js,jsx,vue}",
'!components/*/style/index.{js,jsx}',
'!components/style/index.{js,jsx}',
'!components/*/locale/index.{js,jsx}',
'!components/*/__tests__/**/type.{js,jsx}',
"!**/node_modules/**"
],
transformIgnorePatterns,
};

View File

@ -5,7 +5,7 @@ node_js:
before_script: before_script:
- npm install vue vue-template-compiler - npm install vue vue-template-compiler
script: script:
- npm test - COVERAGE=true npm run test
- npm run codecov - npm run codecov
- bash ./scripts/deploy-to-gh-pages.sh - bash ./scripts/deploy-to-gh-pages.sh
env: env:

View File

@ -68,7 +68,7 @@ function dist (done) {
} }
function babelify (js, modules) { function babelify (js, modules) {
const babelConfig = getBabelCommonConfig(modules) const babelConfig = { ...getBabelCommonConfig(modules), babelrc: false }
delete babelConfig.cacheDirectory delete babelConfig.cacheDirectory
if (modules === false) { if (modules === false) {
babelConfig.plugins.push(replaceLib) babelConfig.plugins.push(replaceLib)
@ -123,6 +123,7 @@ function compile (modules) {
const source = [ const source = [
'components/**/*.js', 'components/**/*.js',
'components/**/*.jsx', 'components/**/*.jsx',
'!components/*/__tests__/*',
] ]
const jsFilesStream = babelify(gulp.src(source), modules) const jsFilesStream = babelify(gulp.src(source), modules)
return merge2([less, jsFilesStream, assets]) return merge2([less, jsFilesStream, assets])

View File

@ -1,28 +0,0 @@
# `Button`
#### `create primary button`
```
"<button type=\"button\" class=\"ant-btn ant-btn-primary\"><span>按 钮</span></button>"
```
#### `should support link button`
```
"<button target=\"_blank\" href=\"http://ant.design\" type=\"button\" class=\"ant-btn ant-btn-default\"><span>link button</span></button>"
```
#### `fixbug renders {0} , 0 and {false}`
```
"<button type=\"button\" class=\"ant-btn ant-btn-default\"><span>0</span></button>"
```
```
"<button type=\"button\" class=\"ant-btn ant-btn-default\"><span>0</span></button>"
```
```
"<button type=\"button\" class=\"ant-btn ant-btn-default\"></button>"
```

View File

@ -1,140 +0,0 @@
import Button from '../index'
import Icon from '../../icon'
import { mount } from 'avoriaz'
import Vue from 'vue'
import { matchSnapshot } from "chai-karma-snapshot";
import { use, expect, assert } from "chai";
use(matchSnapshot);
describe('Button', () => {
it('create primary button', () => {
const wrapper = mount({
render (h) {
return <Button type='primary'>按钮</Button>
},
})
expect(wrapper.html()).to.matchSnapshot();
expect(wrapper.hasClass('ant-btn-primary')).to.equal(true)
})
it('renders Chinese characters correctly', (done) => {
const wrapper = mount(
{
render (h) {
return <Button>按钮</Button>
},
}
)
expect(wrapper.text()).to.equal('按 钮')
const wrapper1 = mount(
{
render (h) {
return <Button icon='search'>按钮</Button>
},
}
)
expect(wrapper1.text()).to.equal('按钮')
const wrapper2 = mount(
{
render (h) {
return <Button><Icon type="search" />按钮</Button>
},
}
)
expect(wrapper2.text()).to.equal('按钮')
const wrapper3 = mount(
{
render (h) {
return <Button><span>按钮</span></Button>
},
}
)
Vue.nextTick(() => {
expect(wrapper3.find('.ant-btn')[0].hasClass('ant-btn-two-chinese-chars')).to.equal(true);
done()
})
})
it('should change loading state instantly by default', () => {
const DefaultButton = {
data(){
return {
loading: false,
}
},
methods: {
enterLoading () {
this.loading = true
}
},
render() {
return <Button loading={this.loading} onClick={this.enterLoading}>Button</Button>;
}
}
const wrapper = mount(DefaultButton)
wrapper.trigger('click');
Vue.nextTick(() => {
expect(wrapper.find('.ant-btn-loading').length).to.equal(1);
})
});
it('should change loading state with delay', () => {
const DefaultButton = {
data(){
return {
loading: false,
}
},
methods: {
enterLoading () {
this.loading = { delay: 1000 }
}
},
render() {
return <Button loading={this.loading} onClick={this.enterLoading}>Button</Button>;
}
}
const wrapper = mount(DefaultButton)
wrapper.trigger('click');
Vue.nextTick(() => {
expect(wrapper.hasClass('ant-btn-loading').length).to.equal(false);
})
});
it('should support link button', () => {
const wrapper = mount({
render (h) {
return <Button target="_blank" href="http://ant.design">link button</Button>
},
})
expect(wrapper.html()).to.matchSnapshot();
})
it('fixbug renders {0} , 0 and {false}', () => {
const wrapper = mount({
render (h) {
return <Button>{0}</Button>
},
})
expect(wrapper.html()).to.matchSnapshot();
const wrapper1 = mount({
render (h) {
return <Button>0</Button>
},
})
expect(wrapper1.html()).to.matchSnapshot();
const wrapper2 = mount({
render (h) {
return <Button>{false}</Button>
},
})
expect(wrapper2.html()).to.matchSnapshot();
})
})

View File

@ -0,0 +1,162 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders ./components/button/demo/basic.md correctly 1`] = `
<div>
<button type="button" class="ant-btn ant-btn-primary"><span>Primary</span></button>
<button type="button" class="ant-btn ant-btn-default"><span>Default</span></button>
<button type="button" class="ant-btn ant-btn-dashed"><span>Dashed</span></button>
<button type="button" class="ant-btn ant-btn-danger"><span>Danger</span></button>
</div>
`;
exports[`renders ./components/button/demo/button-group.md correctly 1`] = `
<div id="components-button-demo-button-group">
<h4>Basic</h4>
<div class="ant-btn-group">
<button type="button" class="ant-btn ant-btn-default"><span>Cancel</span></button>
<button type="button" class="ant-btn ant-btn-primary"><span>OK</span></button>
</div>
<div class="ant-btn-group">
<button type="button" disabled="disabled" class="ant-btn ant-btn-default"><span>L</span></button>
<button type="button" disabled="disabled" class="ant-btn ant-btn-default"><span>M</span></button>
<button type="button" disabled="disabled" class="ant-btn ant-btn-default"><span>R</span></button>
</div>
<div class="ant-btn-group">
<button type="button" class="ant-btn ant-btn-primary"><span>L</span></button>
<button type="button" class="ant-btn ant-btn-default"><span>M</span></button>
<button type="button" class="ant-btn ant-btn-default"><span>M</span></button>
<button type="button" class="ant-btn ant-btn-dashed"><span>R</span></button>
</div>
<h4>With Icon</h4>
<div class="ant-btn-group">
<button type="button" class="ant-btn ant-btn-primary">
<i class="anticon anticon-left"></i>Go back
</button>
<button type="button" class="ant-btn ant-btn-primary">
Go forward
<i class="anticon anticon-right"></i>
</button>
</div>
<div class="ant-btn-group">
<button type="button" class="ant-btn ant-btn-primary">
<i class="anticon anticon-cloud"></i>
</button>
<button type="button" class="ant-btn ant-btn-primary">
<i class="anticon anticon-cloud-download"></i>
</button>
</div>
</div>
`;
exports[`renders ./components/button/demo/disabled.md correctly 1`] = `
<div>
<button type="button" class="ant-btn ant-btn-primary"><span>Primary</span></button>
<button type="button" disabled="disabled" class="ant-btn ant-btn-primary"><span>Primary(disabled)</span></button>
<br>
<button type="button" class="ant-btn ant-btn-default"><span>Default</span></button>
<button type="button" disabled="disabled" class="ant-btn ant-btn-default"><span>Default(disabled)</span></button>
<br>
<button type="button" class="ant-btn ant-btn-default"><span>Ghost</span></button>
<button type="button" disabled="disabled" class="ant-btn ant-btn-default"><span>Ghost(disabled)</span></button>
<br>
<button type="button" class="ant-btn ant-btn-dashed"><span>Dashed</span></button>
<button type="button" disabled="disabled" class="ant-btn ant-btn-dashed"><span>Dashed(disabled)</span></button>
</div>
`;
exports[`renders ./components/button/demo/ghost.md correctly 1`] = `
<div style="background:rgb(190, 200, 200);padding:26px 16px 16px;">
<button type="button" class="ant-btn ant-btn-primary ant-btn-background-ghost"><span>Primary</span></button>
<button type="button" class="ant-btn ant-btn-default ant-btn-background-ghost"><span>Default</span></button>
<button type="button" class="ant-btn ant-btn-dashed ant-btn-background-ghost"><span>Dashed</span></button>
<button type="button" class="ant-btn ant-btn-danger ant-btn-background-ghost"><span>danger</span></button>
</div>
`;
exports[`renders ./components/button/demo/icon.md correctly 1`] = `
<div>
<button type="button" class="ant-btn ant-btn-primary ant-btn-circle">
<i class="anticon anticon-search"></i>
</button>
<button type="button" class="ant-btn ant-btn-primary">
<i class="anticon anticon-search"></i><span>Search</span></button>
<button type="button" class="ant-btn ant-btn-default ant-btn-circle">
<i class="anticon anticon-search"></i>
</button>
<button type="button" class="ant-btn ant-btn-default">
<i class="anticon anticon-search"></i><span>Search</span></button>
<button type="button" class="ant-btn ant-btn-default ant-btn-circle">
<i class="anticon anticon-search"></i>
</button>
<button type="button" class="ant-btn ant-btn-default">
<i class="anticon anticon-search"></i><span>Search</span></button>
<button type="button" class="ant-btn ant-btn-dashed ant-btn-circle">
<i class="anticon anticon-search"></i>
</button>
<button type="button" class="ant-btn ant-btn-dashed">
<i class="anticon anticon-search"></i><span>Search</span></button>
</div>
`;
exports[`renders ./components/button/demo/loading.md correctly 1`] = `
<div>
<button type="button" class="ant-btn ant-btn-primary ant-btn-loading">
<i class="anticon anticon-loading anticon-spin"></i><span>Loading</span></button>
<button type="button" class="ant-btn ant-btn-primary ant-btn-sm ant-btn-loading">
<i class="anticon anticon-loading anticon-spin"></i><span>Loading</span></button>
<br>
<button type="button" class="ant-btn ant-btn-primary"><span>mouseenter me!</span></button>
<button type="button" class="ant-btn ant-btn-primary">
<i class="anticon anticon-poweroff"></i><span>延迟1s</span></button>
<br>
<button type="button" class="ant-btn ant-btn-default ant-btn-circle ant-btn-loading">
<i class="anticon anticon-loading anticon-spin"></i>
</button>
<button type="button" class="ant-btn ant-btn-primary ant-btn-circle ant-btn-loading">
<i class="anticon anticon-loading anticon-spin"></i>
</button>
</div>
`;
exports[`renders ./components/button/demo/multiple.md correctly 1`] = `
<div>
<button type="button" class="ant-btn ant-btn-primary"><span>Primary</span></button>
<button type="button" class="ant-btn ant-btn-default"><span>secondary</span></button>
<button type="button" class="ant-btn ant-btn-default ant-dropdown-trigger">
Actions
<i class="anticon anticon-down"></i>
</button>
</div>
`;
exports[`renders ./components/button/demo/size.md correctly 1`] = `
<div>
<div class="ant-radio-group ant-radio-group-default">
<label class="ant-radio-button-wrapper ant-radio-button-wrapper-checked"><span class="ant-radio-button ant-radio-button-checked"><input type="radio" checked="checked" class="ant-radio-button-input"><span class="ant-radio-button-inner"></span></span><span>Large</span></label>
<label class="ant-radio-button-wrapper"><span class="ant-radio-button"><input type="radio" class="ant-radio-button-input"><span class="ant-radio-button-inner"></span></span><span>Default</span></label>
<label class="ant-radio-button-wrapper"><span class="ant-radio-button"><input type="radio" class="ant-radio-button-input"><span class="ant-radio-button-inner"></span></span><span>Small</span></label>
</div>
<br>
<br>
<button type="button" class="ant-btn ant-btn-primary ant-btn-lg"><span>Primary</span></button>
<button type="button" class="ant-btn ant-btn-default ant-btn-lg"><span>Normal</span></button>
<button type="button" class="ant-btn ant-btn-dashed ant-btn-lg"><span>Dashed</span></button>
<button type="button" class="ant-btn ant-btn-danger ant-btn-lg"><span>Danger</span></button>
<br>
<button type="button" class="ant-btn ant-btn-primary ant-btn-circle ant-btn-lg">
<i class="anticon anticon-download"></i>
</button>
<button type="button" class="ant-btn ant-btn-primary ant-btn-lg">
<i class="anticon anticon-download"></i><span>Download</span></button>
<br>
<div class="ant-btn-group ant-btn-group-lg">
<button type="button" class="ant-btn ant-btn-primary">
<i class="anticon anticon-left"></i>Backward
</button>
<button type="button" class="ant-btn ant-btn-primary">
Forward
<i class="anticon anticon-right"></i>
</button>
</div>
</div>
`;

View File

@ -0,0 +1,3 @@
import demoTest from '../../../tests/shared/demoTest'
demoTest('button')

View File

@ -0,0 +1,135 @@
import Button from '../index'
import Icon from '../../icon'
import { mount } from '@vue/test-utils'
import Vue from 'vue'
describe('Button', () => {
it('create primary button', () => {
const wrapper = mount({
render (h) {
return <Button type='primary'>按钮</Button>
},
})
expect(wrapper.contains('.ant-btn-primary')).toBe(true)
})
// it('renders Chinese characters correctly', (done) => {
// const wrapper = mount(
// {
// render (h) {
// return <Button>按钮</Button>
// },
// }
// )
// expect(wrapper.text()).to.equal('按 钮')
// const wrapper1 = mount(
// {
// render (h) {
// return <Button icon='search'>按钮</Button>
// },
// }
// )
// expect(wrapper1.text()).to.equal('按钮')
// const wrapper2 = mount(
// {
// render (h) {
// return <Button><Icon type="search" />按钮</Button>
// },
// }
// )
// expect(wrapper2.text()).to.equal('按钮')
// const wrapper3 = mount(
// {
// render (h) {
// return <Button><span>按钮</span></Button>
// },
// }
// )
// Vue.nextTick(() => {
// expect(wrapper3.find('.ant-btn')[0].hasClass('ant-btn-two-chinese-chars')).to.equal(true);
// done()
// })
// })
// it('should change loading state instantly by default', () => {
// const DefaultButton = {
// data(){
// return {
// loading: false,
// }
// },
// methods: {
// enterLoading () {
// this.loading = true
// }
// },
// render() {
// return <Button loading={this.loading} onClick={this.enterLoading}>Button</Button>;
// }
// }
// const wrapper = mount(DefaultButton)
// wrapper.trigger('click');
// Vue.nextTick(() => {
// expect(wrapper.find('.ant-btn-loading').length).to.equal(1);
// })
// });
// it('should change loading state with delay', () => {
// const DefaultButton = {
// data(){
// return {
// loading: false,
// }
// },
// methods: {
// enterLoading () {
// this.loading = { delay: 1000 }
// }
// },
// render() {
// return <Button loading={this.loading} onClick={this.enterLoading}>Button</Button>;
// }
// }
// const wrapper = mount(DefaultButton)
// wrapper.trigger('click');
// Vue.nextTick(() => {
// expect(wrapper.hasClass('ant-btn-loading').length).to.equal(false);
// })
// });
// it('should support link button', () => {
// const wrapper = mount({
// render (h) {
// return <Button target="_blank" href="http://ant.design">link button</Button>
// },
// })
// expect(wrapper.html()).to.matchSnapshot();
// })
// it('fixbug renders {0} , 0 and {false}', () => {
// const wrapper = mount({
// render (h) {
// return <Button>{0}</Button>
// },
// })
// expect(wrapper.html()).to.matchSnapshot();
// const wrapper1 = mount({
// render (h) {
// return <Button>0</Button>
// },
// })
// expect(wrapper1.html()).to.matchSnapshot();
// const wrapper2 = mount({
// render (h) {
// return <Button>{false}</Button>
// },
// })
// expect(wrapper2.html()).to.matchSnapshot();
// })
})

View File

@ -23,7 +23,7 @@
], ],
"scripts": { "scripts": {
"start": "NODE_ENV=development ./node_modules/.bin/webpack-dev-server --open --hot", "start": "NODE_ENV=development ./node_modules/.bin/webpack-dev-server --open --hot",
"test": "cross-env BABEL_ENV=test karma start test/karma.conf.js --single-run", "test": "jest --config .jest.js",
"site": "node scripts/run.js site-dist", "site": "node scripts/run.js site-dist",
"copy": "node scripts/run.js copy-html", "copy": "node scripts/run.js copy-html",
"compile": "node antd-tools/cli/run.js compile", "compile": "node antd-tools/cli/run.js compile",
@ -54,13 +54,14 @@
"vue-template-compiler": ">=2.5.0" "vue-template-compiler": ">=2.5.0"
}, },
"devDependencies": { "devDependencies": {
"@octokit/rest": "^15.2.6", "@vue/server-test-utils": "^1.0.0-beta.16",
"@vue/test-utils": "^1.0.0-beta.16",
"autoprefixer": "^8.1.0", "autoprefixer": "^8.1.0",
"avoriaz": "^6.3.0",
"babel-cli": "^6.26.0", "babel-cli": "^6.26.0",
"babel-core": "^6.26.0", "babel-core": "^6.26.0",
"babel-eslint": "^8.0.1", "babel-eslint": "^8.0.1",
"babel-helper-vue-jsx-merge-props": "^2.0.3", "babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.2", "babel-loader": "^7.1.2",
"babel-plugin-add-module-exports": "^0.2.1", "babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-import": "^1.1.1", "babel-plugin-import": "^1.1.1",
@ -79,8 +80,6 @@
"babel-polyfill": "^6.26.0", "babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1", "babel-preset-env": "^1.6.1",
"case-sensitive-paths-webpack-plugin": "^2.1.2", "case-sensitive-paths-webpack-plugin": "^2.1.2",
"chai": "^4.1.2",
"chai-karma-snapshot": "^0.7.0",
"chalk": "^2.3.2", "chalk": "^2.3.2",
"cheerio": "^1.0.0-rc.2", "cheerio": "^1.0.0-rc.2",
"codecov": "^3.0.0", "codecov": "^3.0.0",
@ -96,6 +95,7 @@
"eslint-plugin-vue-libs": "^1.2.1", "eslint-plugin-vue-libs": "^1.2.1",
"extract-text-webpack-plugin": "^3.0.2", "extract-text-webpack-plugin": "^3.0.2",
"fetch-jsonp": "^1.1.3", "fetch-jsonp": "^1.1.3",
"glob": "^7.1.2",
"gulp": "^3.9.1", "gulp": "^3.9.1",
"gulp-babel": "^7.0.0", "gulp-babel": "^7.0.0",
"gulp-strip-code": "^0.1.4", "gulp-strip-code": "^0.1.4",
@ -103,20 +103,9 @@
"html-webpack-plugin": "^2.30.1", "html-webpack-plugin": "^2.30.1",
"husky": "^0.14.3", "husky": "^0.14.3",
"istanbul-instrumenter-loader": "^3.0.0", "istanbul-instrumenter-loader": "^3.0.0",
"jest": "^22.4.3",
"jest-serializer-vue": "^1.0.0",
"jsonp": "^0.2.1", "jsonp": "^0.2.1",
"karma": "^2.0.2",
"karma-babel-preprocessor": "^7.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-coverage-istanbul-reporter": "^1.3.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"karma-mocha-snapshot": "^0.2.1",
"karma-sinon-chai": "^1.3.1",
"karma-snapshot": "^0.6.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.31",
"karma-webpack": "^2.0.13",
"less": "^2.7.2", "less": "^2.7.2",
"less-loader": "^4.0.5", "less-loader": "^4.0.5",
"less-plugin-npm-import": "^2.1.0", "less-plugin-npm-import": "^2.1.0",
@ -149,8 +138,10 @@
"vue": "^2.5.16", "vue": "^2.5.16",
"vue-antd-md-loader": "^1.0.3", "vue-antd-md-loader": "^1.0.3",
"vue-clipboard2": "0.0.8", "vue-clipboard2": "0.0.8",
"vue-jest": "^2.5.0",
"vue-loader": "^13.0.5", "vue-loader": "^13.0.5",
"vue-router": "^3.0.1", "vue-router": "^3.0.1",
"vue-server-renderer": "^2.5.16",
"vue-template-compiler": "^2.5.16", "vue-template-compiler": "^2.5.16",
"webpack": "^3.11.0", "webpack": "^3.11.0",
"webpack-chunk-hash": "^0.5.0", "webpack-chunk-hash": "^0.5.0",

View File

@ -1,12 +0,0 @@
{
"extends": ["plugin:vue-libs/recommended"],
"rules": {
"comma-dangle": [2, "always-multiline"],
"no-var": "error"
},
"globals": {
"it": true,
"describe": true,
"expect": true
}
}

View File

@ -1,14 +0,0 @@
import Vue from 'vue'
Vue.config.productionTip = false
// require all test files (files that ends with .spec.js)
const testsContext = require.context(`../components/${process.env.SCOPE}`, true, /\.spec$/)
testsContext.keys().forEach(testsContext)
// require all src files except main.js for coverage.
// you can also change this to match only the subset of files that
// you want coverage for.
const srcContext = require.context(`../components/${process.env.SCOPE}`, true, /^\.(\.js|\.jsx)?$/)
srcContext.keys().forEach(srcContext)

View File

@ -1,63 +0,0 @@
// This is a karma config file. For more details see
// http://karma-runner.github.io/0.13/config/configuration-file.html
// we are also using it with karma-webpack
// https://github.com/webpack/karma-webpack
const path = require('path')
const webpack = require('webpack')
const webpackConfig = require('../webpack.config')
const merge = require('webpack-merge')
delete webpackConfig.entry
const scope = process.argv[5] || ''
process.env.CHROME_BIN = require('puppeteer').executablePath()
function resolve (basePath, suiteName = '') {
return path.join(basePath, '../components', suiteName.toLowerCase(), '__test__/__snapshots__', 'index.test.md')
}
module.exports = function (config) {
config.set({
// to run in additional browsers:
// 1. install corresponding karma launcher
// http://karma-runner.github.io/0.13/config/browsers.html
// 2. add it to the `browsers` array below.
browsers: ['ChromeHeadless'],
frameworks: ['mocha', 'snapshot', 'mocha-snapshot', 'sinon-chai'],
reporters: ['spec', 'coverage'],
files: ['../components/**/__snapshots__/**/*.md', './index.js'],
preprocessors: {
'../components/**/__snapshots__/**/*.md': ['snapshot'],
'./index.js': ['webpack', 'sourcemap'],
},
port: 9876,
colors: true,
autoWatch: true,
webpack: merge(
{
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"testing"',
SCOPE: `"${scope}"`,
},
}),
],
},
webpackConfig
),
webpackMiddleware: {
noInfo: true,
},
snapshot: {
update: !!process.env.UPDATE,
prune: !!process.env.PRUNE,
pathResolver: resolve, // Custom path resolver,
},
coverageReporter: {
dir: './coverage',
reporters: [
{ type: 'lcov', subdir: '.' },
{ type: 'text-summary' },
],
includeAllSources: false,
},
})
}

View File

@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`antd dist files exports modules correctly 1`] = `
Array [
"version",
"install",
"message",
"notification",
"Affix",
"AutoComplete",
"Alert",
"Avatar",
"BackTop",
"Badge",
"Breadcrumb",
"Button",
"Calendar",
"Card",
"Collapse",
"Cascader",
"Checkbox",
"Col",
"DatePicker",
"Divider",
"Dropdown",
"Form",
"Icon",
"Input",
"InputNumber",
"Layout",
"LocaleProvider",
"Menu",
"Modal",
"Pagination",
"Popconfirm",
"Popover",
"Progress",
"Radio",
"Rate",
"Row",
"Select",
"Slider",
"Spin",
"Steps",
"Switch",
"Table",
"Transfer",
"Tree",
"Tabs",
"Tag",
"TimePicker",
"Timeline",
"Tooltip",
"Upload",
"default",
]
`;

21
tests/index.test.js Normal file
View File

@ -0,0 +1,21 @@
import pkg from '../package.json'
const testDist = process.env.LIB_DIR === 'dist'
describe('antd dist files', () => {
// https://github.com/ant-design/ant-design/issues/1638
// https://github.com/ant-design/ant-design/issues/1968
it('exports modules correctly', () => {
const antd = testDist ? require('../dist/antd') : require('../components') // eslint-disable-line global-require
expect(Object.keys(antd)).toMatchSnapshot()
})
// https://github.com/ant-design/ant-design/issues/1970
// https://github.com/ant-design/ant-design/issues/1804
if (testDist) {
it('should have antd.version', () => {
const antd = require('../dist/antd') // eslint-disable-line global-require
expect(antd.version).toBe(pkg.version)
})
}
})

22
tests/setup.js Normal file
View File

@ -0,0 +1,22 @@
import Vue from 'vue'
import antd from 'vue-antd-ui'
Vue.use(antd)
/* eslint-disable global-require */
if (typeof window !== 'undefined') {
global.window.resizeTo = (width, height) => {
global.window.innerWidth = width || global.window.innerWidth
global.window.innerHeight = height || global.window.innerHeight
global.window.dispatchEvent(new Event('resize'))
}
}
// The built-in requestAnimationFrame and cancelAnimationFrame not working with jest.runFakeTimes()
// https://github.com/facebook/jest/issues/5147
global.requestAnimationFrame = function (cb) {
return setTimeout(cb, 0)
}
global.cancelAnimationFrame = function (cb) {
return clearTimeout(cb, 0)
}

22
tests/shared/demoTest.js Normal file
View File

@ -0,0 +1,22 @@
import glob from 'glob'
import { renderToString } from '@vue/server-test-utils'
import MockDate from 'mockdate'
import moment from 'moment'
export default function demoTest (component, options = {}) {
const files = glob.sync(`./components/${component}/demo/*.md`)
files.forEach((file) => {
let testMethod = options.skip === true ? test.skip : test
if (Array.isArray(options.skip) && options.skip.some(c => file.includes(c))) {
testMethod = test.skip
}
testMethod(`renders ${file} correctly`, () => {
MockDate.set(moment('2016-11-22'))
const demo = require(`../.${file}`).default || require(`../.${file}`)// eslint-disable-line global-require, import/no-dynamic-require
const wrapper = renderToString(demo)
expect(wrapper).toMatchSnapshot()
MockDate.reset()
})
})
}

10
tests/utils.js Normal file
View File

@ -0,0 +1,10 @@
import moment from 'moment'
import MockDate from 'mockdate'
export function setMockDate (dateString = '2017-09-18T03:30:07.795') {
MockDate.set(moment(dateString))
}
export function resetMockDate () {
MockDate.reset()
}