Импорт кукловодов в жасминовых тестах (с глотком) - PullRequest
0 голосов
/ 19 октября 2019

Я новичок во всем мире "создания и тестирования" веб-приложений, поэтому заранее извиняюсь, если скажу что-нибудь глупое.

Я хочу написать несколько юнит-тестов с использованием Jasmine, и мне нужно имитировать щелчкипоэтому я решил использовать puppeteer.

Я установил пакет через npm (npm install puppeteer и npm install puppeteer-core) и в свой gulpfile.js добавил const puppeteer = require('puppeteer-core');. Затем я добавил import puppeteer from 'puppeteer'; в свой тестовый файл, но все, что я получаю, это сообщение об ошибке Uncaught SyntaxError: Невозможно использовать оператор импорта вне модуля .

Моя структура папок имеет видэто следует:

  • Project
    • index.html
    • gulpfile.js
    • images
    • css
    • dist
      • index.html
      • js
        • all.js
        • bundle.js
    • js
      • app.js
      • defaults.js
    • жасмин
      • spec
        • тесты.js

Мой gulpfile.js:

const {dest, src, task, watch, series, parallel} = require("gulp");
const autoprefixer = require("gulp-autoprefixer");
const browserSync = require('browser-sync').create();
const browserify = require("browserify");
const babelify = require("babelify");
const source = require("vinyl-source-stream");
const eslint = require("gulp-eslint");
const concat = require("gulp-concat");
const jasmineBrowser = require('gulp-jasmine-browser');
const puppeteer = require('puppeteer-core');

/**
 * Convert ES6 ode in all js files in src/js folder and copy to
 * build folder as bundle.js
 */
const build = done => {
    browserify({
        "debug": true,
        "entries": ["./js/app.js"]
    }).transform(babelify.configure({"presets": ["@babel/preset-env"]}))
      .bundle()
      .pipe(source("bundle.js"))
      .pipe(dest("./dist/js"));

    done();
};

const lint = done => {
    src(['js/**/*.js'])
        // eslint() attaches the lint output to the eslint property
        // of the file object so it can be used by other modules.
        .pipe(eslint({
            "parserOptions": {
                "ecmaVersion": 2018,
                "sourceType": "module"
            }
        }))
        // eslint.format() outputs the lint results to the console.
        // Alternatively use eslint.formatEach() (see Docs).
        .pipe(eslint.format())
        // To have the process exit with an error code (1) on
        // lint error, return the stream and pipe to failOnError last.
        .pipe(eslint.failOnError());

    done();
};

const testInConsole = () => src('tests/spec/extraSpec.js')
                           .pipe(jasmineBrowser.specRunner({"console": true}))
                           .pipe(jasmineBrowser.headless({"driver": "chrome"}));

const testInBrowser = () => src('tests/spec/extraSpec.js')
                            .pipe(jasmineBrowser.specRunner())
                            .pipe(jasmineBrowser.server({"port": 3001}));

const copyHtml = () => src("./index.html").pipe(dest("./dist"));

const copyImages = () => src("./images/**/*").pipe(dest("./dist/images"));

const scriptsDist = () => src("js/**/*.js")
                          .pipe(concat("all.js"))
                          .pipe(dest("dist/js"));

const browserSyncTask = done => {
    browserSync.init({"server": "./dist"});
    done();
};

const watcher = done => {
    watch("js/**/*.js", build);
    watch("index.html", copyHtml);

    done();
};

const test = () => src(['js/**/*.js', 'jasmine/spec/**/*.js'])
                   .pipe(jasmineBrowser.specRunner())
                   .pipe(jasmineBrowser.server({"port": 8888}))

task("copyHtml", copyHtml);
task("copyImages", copyImages);
task("lint", lint);
task("fullBuild", series(parallel(copyHtml, copyImages, scriptsDist), build));
task("build", build);
task("test", testInBrowser);
task("watch", series(watcher, browserSyncTask));
task("default", parallel(lint, build));
task("scriptsDist", scriptsDist);
task("test", test);

Кроме того, на отдельной заметкеЯ пытался импортировать что-то из default.js в tests.js, но опять же он не импортировал бы, выдавая ту же ошибку ...

Заранее спасибо за помощь!

...