【问题标题】:Issue a unit test on a Gulp file using Karma & Jasmine使用 Karma 和 Jasmine 对 Gulp 文件进行单元测试
【发布时间】:2015-11-12 19:58:27
【问题描述】:

我为 Gulp 文件编写了一个单元测试。该测试使用 Jasmine 框架 (safari/chrome) 通过业力运行。 知道 Karma 在浏览器中运行 json 配置文件中指定的测试文件,它应该有办法在浏览器中运行 Node 的模块。 经过研究,我发现通过使用 Browserify,我将能够在浏览器中加载模块。

当我写这个命令时

浏览 ./Test/GulpTest.js -o ./Test/TEST.js -d

新的 TEST.js 好像很大

然后我运行以下命令在新的 TEST.js 上开始测试

业力启动 karma.conf.js

我收到此错误:

gulp 输入流 ✗ 应该从 ts 编译到 js TypeError: Cannot read property 'isTTY' of undefined 在对象。 (/Users/Snap/Desktop/Demo App/Test/TEST.js:75226:20) 在 Object.252._process (/Users/Snap/Desktop/Demo App/Test/TEST.js:75284:4) 在 s (/Users/Snap/Desktop/Demo App/Test/TEST.js:1:254) 在 /Users/Snap/Desktop/Demo App/Test/TEST.js:1:305 在 Object.31../lib/PluginError (/Users/Snap/Desktop/Demo App/Test/TEST.js:3530:9) 在 Object.239../lib/PluginError (/Users/Snap/Desktop/Demo App/Test/TEST.js:75113:21) 在 s (/Users/Snap/Desktop/Demo App/Test/TEST.js:1:254) 在 /Users/Snap/Desktop/Demo App/Test/TEST.js:1:305 在 Object.229.deprecated (/Users/Snap/Desktop/Demo App/Test/TEST.js:74824:13) 在 s (/Users/Snap/Desktop/Demo App/Test/TEST.js:1:254)

Chrome 44.0.2403 (Mac OS X 10.10.4):执行 1 of 1 (1 FAILED) 错误 (0.037 秒 / 0.03 秒)

    it("should compile from ts to js", function () {
            var gulp = require("gulp");
            var ts = require("gulp-typescript");
            var lazy = require("gulp-load-plugins");
            var fs = require('graceful-fs');
            var should = require('should');
            var join = require('path').join;


            /*
            * * * Compile Typescript to JavaScript 
            */
            gulp.task("ts-compiler", function () {
                return gulp.src("./Test/lib/file.ts")
                           .pipe(lazy.typescript({
                                // Generates corresponding .map file. 
                                sourceMap : false,

                                // Generates corresponding .d.ts file. 
                                declaration : true,

                                // Do not emit comments to output. 
                                removeComments : false,

                                // Warn on expressions and declarations with an implied 'any' type. 
                                noImplicitAny : false,

                                // Skip resolution and preprocessing. 
                                noResolve : false,

                                // Specify module code generation: 'commonjs' or 'amd'   
                                module : "amd",

                                // Specify ECMAScript target version: 'ES3' (default), or 'ES5' 
                                target : "ES5"
                            }))
                            .pipe(gulp.dest("./Test/lib/dest"));
            });

            gulp.start("ts-compiler", function () {
                console.log("compiling...");
                should.exist("./Test/lib/dest/file.js");
            });

});

【问题讨论】:

    标签: javascript jasmine gulp karma-runner browserify


    【解决方案1】:

    你不能在 karma 和 jasmine 中运行 Gulp——你运行它就像是一个前端库,但它不是。

    您需要使用后端测试库。你仍然可以使用 jasmine,你只需要通过 nodejs 来使用它,gulp 就是这样运行的:https://www.npmjs.com/package/jasmine

    您可能有兴趣了解我如何为我工作的 gulp 任务编写单元测试:https://github.com/Lostmyname/lmn-gulp-tasks/tree/master/test

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多