webpack Jasmine beforeEach未等待完成回调

axzmvihb  于 2022-11-13  发布在  Webpack
关注(0)|答案(2)|浏览(157)

我对Jasmine有问题(+Karma + Webpack)
我将测试范围缩小到beforeEach语句,在运行it块之前不等待执行done()回调。

**编辑:**Navjot Ahuja指出jasmine-node在beforeEach中与setTimeout有问题,所以我将其改为使用promises,与他的建议略有不同(正如他的作品,但本示例没有)

以下是我发现的一些奇怪的事情:

  • 测试似乎被打印了两次。我看过一些关于这个的帖子,所以它可能只是简单地使用了spec插件(或点)。
  • 我在下面的测试运行程序浏览器窗口中看到了一些jasmine错误。我怀疑这可能是问题的原因,但我不知道如何解决。
  • 如果我改用beforeAll,它就可以工作(但这不是一个选项)

这是测试:

describe('Test', function () {
    let flag = false;

    describe('simple test -', function () {
        beforeEach(function (done) {
            console.log('SETTING TIMEOUT');

            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    flag = true;
                    done();
                }
            };
            xhttp.open('GET', 'https://api.github.com/zen', true);
            xhttp.send();
        });
        it('should wait for done and set flag correctly', function () {
            console.log('INSIDE IT STATEMENT');
            expect(flag).toBe(true);
        });
    });
});

这是karma.conf.js

// Karma configuration
const webpackTestConfig = require('./build/tests.webpack.config.js');

module.exports = function (config) {
    const webpackConfig = webpackTestConfig;

    config.set({
        client: {
            args: [
                `--ver=${config.ver}`,
                `--region=${config.region}`,
                `--env=${config.env}`
            ],
            captureConsole: true
        },

        basePath: '',
        frameworks: ['jasmine'],
        files: [
            'tests/integration/integrationTests.js'
        ],

        reporters: ['spec', 'junit'],
        junitReporter: {
            useBrowserName: false,
            outputFile: 'reports/js/unit-components/results/ITESTS-junit.xml'
        },

        coverageReporter: {
            reporters: [
                {type: 'text-summary', dir: 'reports/js/unit-components/coverage/text-summary/', subdir: '.'},
                {type: 'lcov', dir: 'reports/js/unit-components/coverage/lcov/', subdir: '.'},
                {type: 'html', dir: 'reports/js/unit-components/coverage/html', subdir: '.'},
                {type: 'cobertura', dir: 'reports/js/unit-components/coverage/cobertura', subdir: '.'}
            ]
        },

        preprocessors: {
            'tests/integration/integrationTests.js': ['webpack']
        },
        webpack: webpackConfig,
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        captureTimeout: 60000,
        browserNoActivityTimeout: 600000,
        singleRun: false
    });
};

这是test.webpack.config.js

/*global __dirname*/
const webpackConfigBase = require('./webpack.config.js');
const path = require('path');
const _ = require('underscore');
const rootDir = path.resolve(__dirname, '..');

module.exports = () => {
    const config = webpackConfigBase();

    _.extend(config.resolve.alias, {
        'integrationTests': path.join(rootDir, 'tests/integration'),
        'testUtils': path.join(rootDir, 'tests/util'),
        'testFixtures': path.join(rootDir, 'tests/fixture')
    });

    return config;
};

这是参考的基本Webpack配置:

/* globals __dirname */
const path = require('path');
const rootDir = path.resolve(__dirname, '..');

function matchModule(resource, moduleName) {
    return resource && resource.indexOf(moduleName) >= 0;
}

module.exports = () => ({
    context: path.join(rootDir, 'www/latest/'),
    entry: './app/api.js',
    resolve: {
        modules: [
            path.join(rootDir, 'www/latest/app'),
            path.join(rootDir, 'node_modules')
        ],
        alias: {
            'vendor': path.join(rootDir, 'www/latest/vendor')
            // other paths here but removed
        }
    },
    output: {
        path: path.join(rootDir, 'www/latest/dist/'),
        publicPath: 'CENSOREDPATH',
        filename: 'api.js',
        devtoolModuleFilenameTemplate: 'applicationname://[resource-path]',
        devtoolFallbackModuleFilenameTemplate: 'applicationname://[resource-path]?[hash]'
    },
    module: {
        rules: [
            {
                enforce: 'pre',
                test: /\.js$/,
                loader: 'eslint-loader'
            },
            {
                test: /\.js$/,
                exclude: function (resource) {
                    const es6Vendors = [
                        'vendor/censored/src',
                        'vendor/babel'
                    ];
                    const isNodeModule = matchModule(resource, 'node_modules');
                    const isVendor = matchModule(resource, 'vendor');
                    const isEs6Vendor = (es6Vendors.filter(vendor => matchModule(resource, vendor)).length >= 1);

                    if (isNodeModule || (isVendor && !isEs6Vendor)) {
                        return true;
                    }

                    return false;
                },
                loader: 'babel-loader'
            }
        ]
    },
    externals: {
        'react/addons': true,
        'react/lib/ExecutionEnvironment': true,
        'react/lib/ReactContext': true
    },
    node: {
        setImmediate: false,
        clearImmediate: false
    }
});

=========================================================================
以下是控制台的输出:

Version: webpack 3.6.0
Time: 39ms
webpack: Compiled successfully.
    webpack: Compiling...
06 10 2017 07:30:01.789:WARN [karma]: No captured browser, open http://localhost:9876/
    Hash: 020b93689ec1147a0c3b
Version: webpack 3.6.0
Time: 70ms
Asset     Size  Chunks             Chunk Names
tests/integration/integrationTests.js  3.96 kB       0  [emitted]  tests/integration/integrationTests.js
    [0] ./tests/integration/integrationTests.js 238 bytes {0} [built]
    [1] ./tests/integration models\/item.spec.js$ 192 bytes {0} [built]
    [2] ./tests/integration/specs/models/item.spec.js 612 bytes {0} [optional] [built]
webpack: Compiled successfully.
06 10 2017 07:30:01.799:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
    06 10 2017 07:30:01.800:INFO [launcher]: Launching browser Chrome with unlimited concurrency
06 10 2017 07:30:01.812:INFO [launcher]: Starting browser Chrome
06 10 2017 07:30:02.238:INFO [Chrome 61.0.3163 (Linux 0.0.0)]: Connected on socket mL2CqoCdzxFggtuIAAAA with id 72267743
Chrome 61.0.3163 (Linux 0.0.0) LOG LOG: '=================', './specs/models/item.spec.js', '================='
LOG LOG: 'SETTING TIMEOUT'
LOG LOG: 'INSIDE IT STATEMENT'

Test
simple test -
Test
simple test -
✗ should wait for done and set flag correctly
Expected false to be true.
    at UserContext.<anonymous> (tests/integration/integrationTests.js:121:30)

✗ should wait for done and set flag correctly
Expected false to be true.
    at UserContext.<anonymous> (tests/integration/integrationTests.js:121:30)

Chrome 61.0.3163 (Linux 0.0.0): Executed 2 of 1 (2 FAILED) ERROR (0.007 secs / 0.003 secs)

上述控制台错误中值得注意的错误包括:

  1. Uncaught TypeError: Cannot read property 'spies' of undefined
  2. Uncaught Error: Tried to complete the wrong suite
    而全输出:
================= ./specs/models/item.spec.js =================
integrationTests.js:120 asdfasdf

debug.js:15 FAILED Test simple test - Test simple test - should wait for done and set flag correctly

debug.js:15 FAILED Test simple test - should wait for done and set flag correctly

2debug.js:6 Skipped 0 tests

integrationTests.js:120 asdfasdf
debug.js:21 Uncaught Expected false to be true.
    at UserContext.<anonymous> (http://karma.vg.censored.com/base/tests/integration/integrationTests.js:121:26)

    (anonymous) @ debug.js:21
setTimeout (async)
(anonymous) @ debug.js:20
window.__karma__.result @ debug.js:23
KarmaReporter.specDone @ adapter.js:243
dispatch @ jasmine.js:4366
(anonymous) @ jasmine.js:4337
specResultCallback @ jasmine.js:1175
complete @ jasmine.js:530
(anonymous) @ jasmine.js:4231
channel.port1.onmessage @ jasmine.js:1774
debug.js:21 Uncaught Expected false to be true.
    at UserContext.<anonymous> (http://karma.vg.censored.com/base/tests/integration/integrationTests.js:121:26)
    (anonymous) @ debug.js:21
setTimeout (async)
(anonymous) @ debug.js:20
window.__karma__.result @ debug.js:23
KarmaReporter.specDone @ adapter.js:243
dispatch @ jasmine.js:4366
(anonymous) @ jasmine.js:4337
specResultCallback @ jasmine.js:1175
complete @ jasmine.js:530
(anonymous) @ jasmine.js:4231
channel.port1.onmessage @ jasmine.js:1774

jasmine.js:1024 Uncaught TypeError: Cannot read property 'spies' of undefined
at currentSpies (jasmine.js:1024)
at SpyRegistry.clearSpies (jasmine.js:4848)
at clearResourcesForRunnable (jasmine.js:818)
at Spec.specResultCallback [as resultCallback] (jasmine.js:1173)
at QueueRunner.complete [as onComplete] (jasmine.js:530)
at jasmine.js:4231
at MessagePort.channel.port1.onmessage (jasmine.js:1774)
currentSpies @ jasmine.js:1024
SpyRegistry.clearSpies @ jasmine.js:4848
clearResourcesForRunnable @ jasmine.js:818
specResultCallback @ jasmine.js:1173
complete @ jasmine.js:530
(anonymous) @ jasmine.js:4231
channel.port1.onmessage @ jasmine.js:1774

jasmine.js:1024 Uncaught TypeError: Cannot read property 'spies' of undefined
at currentSpies (jasmine.js:1024)
at SpyRegistry.clearSpies (jasmine.js:4848)
at clearResourcesForRunnable (jasmine.js:818)
at nodeComplete (jasmine.js:955)
at QueueRunner.onComplete (jasmine.js:5327)
at jasmine.js:4231
at MessagePort.channel.port1.onmessage (jasmine.js:1774)
currentSpies @ jasmine.js:1024
SpyRegistry.clearSpies @ jasmine.js:4848
clearResourcesForRunnable @ jasmine.js:818
nodeComplete @ jasmine.js:955
onComplete @ jasmine.js:5327
(anonymous) @ jasmine.js:4231
channel.port1.onmessage @ jasmine.js:1774

jasmine.js:951 Uncaught Error: Tried to complete the wrong suite
at nodeComplete (jasmine.js:951)
at QueueRunner.onComplete (jasmine.js:5327)
at jasmine.js:4231
at MessagePort.channel.port1.onmessage (jasmine.js:1774)
nodeComplete @ jasmine.js:951
onComplete @ jasmine.js:5327
(anonymous) @ jasmine.js:4231
channel.port1.onmessage @ jasmine.js:1774

jasmine.js:1024 Uncaught TypeError: Cannot read property 'spies' of undefined
at currentSpies (jasmine.js:1024)
at SpyRegistry.clearSpies (jasmine.js:4848)
at clearResourcesForRunnable (jasmine.js:818)
at QueueRunner.onComplete (jasmine.js:984)
at jasmine.js:4231
at MessagePort.channel.port1.onmessage (jasmine.js:1774)
currentSpies @ jasmine.js:1024
SpyRegistry.clearSpies @ jasmine.js:4848
clearResourcesForRunnable @ jasmine.js:818
(anonymous) @ jasmine.js:984
(anonymous) @ jasmine.js:4231
channel.port1.onmessage @ jasmine.js:1774

jasmine.js:2373 Uncaught Uncaught TypeError: Cannot read property 'spies' of undefined

我已经花了很多小时在这上面,现在没有线索,有没有人能给我建议?

rpppsulh

rpppsulh1#

我猜jasmine-node现在不支持超时(问题:(第10页)
你可以用承诺。就像这样:

describe('Test', function() {
  let flag = false;
  let beforeEachPromise;
  describe('simple test -', function() {
    beforeEach(function(done) {
      console.log('SETTING TIMEOUT');
      beforeEachPromise = new Promise(function(resolve, reject) {
        setTimeout(function() {
          console.log('TIMEOUT CALLBACK TRIGGERED');
          flag = true;
          done();
        }, 2000);
      })
    });
    it('should wait for done and set flag correctly', function() {
      beforeEachPromise.then(function() {
        expect(flag).toBe(true);
      })
    });
  });
});
eufgjt7s

eufgjt7s2#

如果您通过搜索Uncaught Error: Tried to complete the wrong suite找到此问题:
1.查看代码中是否有其他错误,方法是在karma配置的顶层设置singleRun: true,然后在浏览器的控制台中查看。
1.查看是否意外导入了jasmine两次。发生这种情况的原因可能是:

  • 除了在karma配置中将jasmine添加到frameworks之外,您还将其添加到files
  • 您正在使用的某个插件已经在加载jasmine,因此您不必在frameworks下指定jasmine。这样的插件示例:@metahub/karma-jasmine-jquery

相关问题