UNPKG

dmp-cli

Version:

Dmp component's cli

79 lines (68 loc) 2.22 kB
const path = require('path') const fs = require('fs') const log = require('./utils/log') const Utils = require('./utils') const shell = require('shelljs') module.exports = function (config, source, args) { if (typeof source !== 'string') { args = source source = process.cwd() } let fstat, cwd = process.cwd(), nmstat source = Utils.isAbsPath(source) && source || path.join(cwd, source) try { fstat = fs.statSync(source) } catch (e) { log.err('source not found', e) return } if (fstat.isDirectory()) { const mainUrl = path.join(source, '/index.js') const configUrl = path.join(source, '/package.json') const testUrl = path.join(source, '/.test/__tests__') try { fstat = fs.statSync(mainUrl) fstat = fs.statSync(configUrl) } catch (e) { log.err('source not found', e) return } try { fstat = fs.statSync(testUrl) } catch (e) { log.err('unit test files not found', e) return } // 执行单元测试 shell.cd(path.join(__dirname, '../')) //动态生成jest配置文件 const jestConfigFile = path.join(__dirname, '../jest/jest_config.json') fs.writeFileSync(jestConfigFile, JSON.stringify({ verbose: true, coverageDirectory: `${source}/.test/__coverage__/`, moduleDirectories: [path.join(__dirname, '../node_modules'), 'node_modules'], testEnvironment: 'jsdom', testURL: 'http://localhost/', transform: { '^.+\\.js?$': path.join(__dirname, '../jest/jest_transformer.js'), '^.+\\.jsx?$': path.join(__dirname, '../jest/jest_transformer.js') }, roots: ['<rootDir>', source] })) // 执行测试 let jestOptions = '' if (args.jest) { const options = args.jest.split(',') if (options && options.length > 0) { jestOptions = options.map(option => `--${option}`) jestOptions = jestOptions && jestOptions.join(' ') } } const jestFilePath = args.jestfilepath || '' shell.exec(`npm run test -- ${jestFilePath} --config ${jestConfigFile} --env=jsdom --no-cache --color --coverage ${jestOptions}`) } else { log.err('unknow type input source', source) } }