3

我有这个gulp任务:

gulp.task('test', function () {
    return gulp.src('test/runner.html')
        .pipe(mochaPhantomJS());
});

这是我的runner.html

<!DOCTYPE html>
<html>
    <head>
        <title>Mocha</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
    </head>
    <body>
        <script src="../node_modules/mocha/mocha.js"></script>
        <script>mocha.setup('bdd')</script>
        <script src="../node_modules/chai/chai.js"></script>
        <script src="../node_modules/requirejs/require.js"></script>

        <script>
          var assert = chai.assert;
          var expect = chai.expect;
          var should = chai.should();
        </script>
        <script src="spec/test.js"></script>
        <script>
            if (window.mochaPhantomJS) {
              console.log('Running mochaPhantomJS...');
              mochaPhantomJS.run();
            } else {
              console.log('Running mocha...');
              mocha.run();
            }
        </script>
    </body>
</html>

这是我的test.js文件:

var chrome = require('sinon-chrome');
var popup = require('../../source/scripts/popup');

describe('sumit', function(){
    before(function () {
        global.chrome = chrome;
    });
    it('Should return 1', function(){
        assert(popup.sum(0,1) === 1);
    });
})

但是当我运行时,gulp test我收到此错误消息:

错误:尚未为上下文加载模块名称“sinon-chrome”:_。使用要求([])

http://requirejs.org/docs/errors.html#notloaded

在 file:///c:/dev/extensions/NEW_EXPRESS/node_modules/requirejs/require.js:1 的 defaultOnError 在 file:///c:/dev/extensions/NEW_EXPRESS/node_modules/requirejs/require.js 的 onError :547 in localRequire 在 file:///c:/dev/extensions/NEW_EXPRESS/node_modules/requirejs/require.js:1433 在 requirejs 在 file:///c:/dev/extensions/NEW_EXPRESS/node_modules/requirejs/require .js:1794

4

2 回答 2

1

错误消息的链接中,它暗示您应该使用异步require方法。

因此,如果您更新test.js到以下内容,那么它应该可以解决该问题:

require(['sinon-chrome'], function (chrome) { 
    var popup = require('../../source/scripts/popup');

    describe('sumit', function(){
        before(function () {
            global.chrome = chrome;
        });
        it('Should return 1', function(){
            assert(popup.sum(0,1) === 1);
        });
    })
});
于 2016-12-01T09:48:39.770 回答
0

在 Angular 7 构建中,您可以做到这一点。

业力配置:

config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular-devkit/build-angular/plugins/karma')
        ],
        client: {
            clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(__dirname, '../coverage'),
            reports: ['html', 'lcovonly'],
            fixWebpackSourcePaths: true
        },
        reporters: ['kjhtml', 'progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false           
    });

在你的测试中:

import * as chrome from 'sinon-chrome';
import {MyService} from '../my.service';

describe('RunTaskService', () => {
    beforeEach(() => {
      TestBed.configureTestingModule({});
      (global as any).chrome = chrome;
    });
    it('should be start a task', done => {
        const service: MyService = TestBed.get(MyService);
        expect(service).toBeTruthy();

        chrome.runtime.lastError = null;
        chrome.tabs.query.yields([{url: 'https://cnn.com', id: 123}]);

        // call your code to test. the chrome.tabs.query will
        // return [{url: 'https://cnn.com', id: 123}]

    });
于 2018-11-01T03:49:32.550 回答