瀏覽代碼

Add Unit Tests Skeleton and gulp tasks

sebastien 7 年之前
父節點
當前提交
4ca0d510cb

+ 2 - 0
.gitignore

@@ -24,6 +24,8 @@ postProcessLibrary/src/**/*.js
 inspector/src/**/*.js
 serializers/src/**/*.js
 gui/src/**/*.js
+tests/unit/**/*.js
+!tests/unit/karma.conf.js
 
 # Build results
 [Rr]elease/

+ 43 - 2
Tools/Gulp/gulpfile.js

@@ -437,8 +437,7 @@ var buildExternalLibrary = function (library, settings, watch) {
  * The default task, concat and min the main BJS files.
  */
 gulp.task("default", function (cb) {
-    runSequence("typescript-all", "intellisense", "typedoc-all", "tests-validation-virtualscreen", "tests-validation-browserstack", cb);
-    // runSequence("typescript-all", "intellisense", "typedoc-all", "tests-validation-virtualscreen", cb);
+    runSequence("typescript-all", "intellisense", "typedoc-all", "tests-unit", "tests-validation-virtualscreen", "tests-validation-browserstack", cb);
 });
 
 gulp.task("mainBuild", function (cb) {
@@ -890,3 +889,45 @@ gulp.task("tests-validation-browserstack", function (done) {
     var server = new karmaServer(kamaServerOptions, done);
     server.start();
 });
+
+/**
+ * Transpiles typescript unit tests. 
+ */
+gulp.task("tests-unit-transpile", function (done) {
+    var tsProject = typescript.createProject('../../tests/unit/tsconfig.json');
+
+    var tsResult = gulp.src("../../tests/unit/**/*.ts", { base: "../../" }) // or tsProject.src()
+        .pipe(tsProject());
+    //var tsResult = tsProject.src().pipe(tsProject());
+ 
+    return tsResult.js.pipe(gulp.dest("../../"));
+});
+
+/**
+ * Launches the KARMA unit tests in phantomJS.
+ * (Can only be launch on any branches.)
+ */
+gulp.task("tests-unit-debug", ["tests-unit-transpile"], function (done) {
+    var kamaServerOptions = {
+        configFile: __dirname + "/../../tests/unit/karma.conf.js",
+        singleRun: false,
+        browsers: ['Chrome']
+    };
+
+    var server = new karmaServer(kamaServerOptions, done);
+    server.start();
+});
+
+/**
+ * Launches the KARMA unit tests in phantomJS.
+ * (Can only be launch on any branches.)
+ */
+gulp.task("tests-unit", ["tests-unit-transpile"], function (done) {
+    var kamaServerOptions = {
+        configFile: __dirname + "/../../tests/unit/karma.conf.js",
+        singleRun: true
+    };
+
+    var server = new karmaServer(kamaServerOptions, done);
+    server.start();
+});

+ 3 - 1
Tools/Gulp/package.json

@@ -45,10 +45,12 @@
         "karma-chrome-launcher": "^2.2.0",
         "karma-firefox-launcher": "^1.1.0",
         "karma-mocha": "^1.3.0",
+        "karma-phantomjs-launcher": "^1.0.4",
         "karma-sinon": "^1.0.5",
         "merge2": "~0.3.5",
         "minimist": "^1.2.0",
         "mocha": "^4.0.1",
+        "phantomjs": "^2.1.7",
         "run-sequence": "~1.1.0",
         "sinon": "^4.1.3",
         "style-loader": "^0.13.2",
@@ -59,6 +61,6 @@
         "webpack-stream": "^4.0.0"
     },
     "scripts": {
-        "install": "npm --prefix ../../Playground/ install ../../Playground/ && gulp deployLocalDev"
+        "install": "npm --prefix ../../Playground/ install ../../Playground/ && npm --prefix ../../tests/unit/ install ../../tests/unit/ && gulp deployLocalDev"
     }
 }

+ 18 - 0
tests/unit/babylon/babylonReference.ts

@@ -0,0 +1,18 @@
+/// <reference path="../../../dist/babylon.d.ts" />
+/// <reference path="../../../dist/loaders/babylon.glTF2FileLoader.d.ts" />
+
+/// <reference path="../node_modules/@types/chai/index.d.ts" />
+/// <reference path="../node_modules/@types/mocha/index.d.ts" />
+/// <reference path="../node_modules/@types/sinon/index.d.ts" />
+
+/*
+ * Create a constant with the ChaiJS' expect module just to make the code more readable.
+ */
+const should = chai.should();
+const expect = chai.expect;
+const assert = chai.assert;
+
+/**
+ * Redirects the devtools used to load the dependencies.
+ */
+declare var BABYLONDEVTOOLS: any;

+ 51 - 0
tests/unit/babylon/loading/babylon.sceneloader.tests.ts

@@ -0,0 +1,51 @@
+/**
+ * Describes the test suite.
+ */
+describe('Babylon Tools', () => {
+    var subject : BABYLON.Engine;
+
+    /**
+     * Loads the dependencies.
+     */
+    before(function (done) {
+        this.timeout(180000);
+        (BABYLONDEVTOOLS).Loader
+            .useDist()
+            .load(function () {
+                done();
+            });
+    });
+
+    /**
+     * Create a nu engine subject before each test.
+     */
+    beforeEach(function () {
+        subject = new BABYLON.NullEngine({
+            renderHeight: 256,
+            renderWidth: 256,
+            textureSize: 256,
+            deterministicLockstep: false,
+            lockstepMaxSteps: 1
+        });
+    });
+
+    /**
+     * This test is more an integration test than a regular unit test but highlights how to rely
+     * on the BABYLON.NullEngine in order to create complex test cases.
+     */
+    describe('#GLTF', () => {
+        it('should load BoomBox GLTF', (done) => {
+            mocha.timeout(10000);
+
+            var scene = new BABYLON.Scene(subject);
+            BABYLON.SceneLoader.Append("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene, function () {
+
+                scene.meshes.length.should.be.equal(2);
+                scene.materials.length.should.be.equal(1);
+                scene.multiMaterials.length.should.be.equal(0);
+
+                done();
+            });
+        });
+    });
+});

+ 44 - 0
tests/unit/babylon/tools/babylon.tools.tests.ts

@@ -0,0 +1,44 @@
+/**
+ * Describes the test suite.
+ */
+describe('Babylon Tools', () => {
+    /**
+     * Loads the dependencies.
+     */
+    before(function (done) {
+        this.timeout(180000);
+        (BABYLONDEVTOOLS).Loader
+            .useDist()
+            .load(function () {
+                done();
+            });
+    });
+
+    /**
+     * This test highlights different ways of using asserts from chai so that you can chose the syntax
+     * you prefer between should, expect, and assert.
+     */
+    describe('#ExponentOfTwo', () => {
+        it('should be expoent of two', () => {
+            var result : boolean = BABYLON.Tools.IsExponentOfTwo(2);
+            expect(result).to.be.true; 
+            
+            result = BABYLON.Tools.IsExponentOfTwo(4);
+            result.should.be.true; 
+            
+            result = BABYLON.Tools.IsExponentOfTwo(8);
+            assert.isTrue(result);
+        });
+
+        it('should not be exponent of two', () => {
+            var result : boolean = BABYLON.Tools.IsExponentOfTwo(3);
+            expect(result).to.be.false; 
+
+            result = BABYLON.Tools.IsExponentOfTwo(6);
+            result.should.be.false;
+
+            result = BABYLON.Tools.IsExponentOfTwo(12);
+            assert.isFalse(result);
+        });
+    });
+});

+ 41 - 0
tests/unit/karma.conf.js

@@ -0,0 +1,41 @@
+module.exports = function (config) {
+  config.set({
+    basePath: '../../',
+        captureTimeout: 3e5,
+        browserNoActivityTimeout: 3e5,
+        browserDisconnectTimeout: 3e5,
+        browserDisconnectTolerance: 3,
+        concurrency: 1,
+
+        urlRoot: '/karma',
+
+        frameworks: ['mocha', 'chai', 'sinon'],
+
+        files: [
+            './Tools/DevLoader/BabylonLoader.js',
+            './tests/unit/**/*.js',
+            { pattern: 'dist/**/*', watched: false, included: false, served: true },
+            { pattern: 'assets/**/*', watched: false, included: false, served: true },
+            { pattern: 'tests/**/*', watched: false, included: false, served: true },
+            { pattern: 'Playground/scenes/**/*', watched: false, included: false, served: true },
+            { pattern: 'Playground/textures/**/*', watched: false, included: false, served: true },
+            { pattern: 'Playground/sounds/**/*', watched: false, included: false, served: true },
+            { pattern: 'Tools/DevLoader/**/*', watched: false, included: false, served: true },
+            { pattern: 'Tools/Gulp/config.json', watched: false, included: false, served: true },
+        ],
+        proxies: {
+            '/': '/base/'
+        },
+
+        port: 3000,
+        colors: true,
+        autoWatch: false,
+        singleRun: false,
+
+        // level of logging
+        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
+        logLevel: config.LOG_INFO,
+
+        browsers: ['PhantomJS']
+  })
+}

+ 16 - 0
tests/unit/package.json

@@ -0,0 +1,16 @@
+{
+  "name": "babylonjsunittest",
+  "version": "6.6.6",
+  "description": "Unit Tests Suite For Babylon.js",
+  "main": "",
+  "repository": {
+      "url": "https://github.com/BabylonJS/Babylon.js/"
+  },
+  "readme": "https://github.com/BabylonJS/Babylon.js/edit/master/readme.md",
+  "license": "(Apache-2.0)",
+  "devDependencies": {
+    "@types/mocha": "2.2.46",
+    "@types/chai": "^4.1.0",
+    "@types/sinon": "^4.1.3"
+  }
+}

+ 9 - 0
tests/unit/tsconfig.json

@@ -0,0 +1,9 @@
+{
+    "compilerOptions": {
+      "target": "es5",
+      "module": "none"
+    },
+    "exclude": [
+      "node_modules"
+    ]
+}