UNPKG

@kubiklabs/wasmkit

Version:

Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.

115 lines (114 loc) 5.15 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.filterNonExistent = void 0; const chalk_1 = __importDefault(require("chalk")); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const ts = __importStar(require("typescript")); const config_env_1 = require("../internal/core/config/config-env"); const errors_1 = require("../internal/core/errors"); const errors_list_1 = require("../internal/core/errors-list"); const project_structure_1 = require("../internal/core/project-structure"); const scripts_1 = require("../lib/compile/scripts"); const files_1 = require("../lib/files"); const task_names_1 = require("./task-names"); function filterNonExistent(scripts) { return scripts.filter(script => !fs_extra_1.default.pathExistsSync(script)); } exports.filterNonExistent = filterNonExistent; async function runTests(runtimeEnv, scriptNames, logDebugTag) { const { default: Mocha } = await Promise.resolve().then(() => __importStar(require('mocha'))); const mocha = new Mocha(runtimeEnv.config.mocha); // log the details of network the test is running on console.log(`Network: { RpcUrl: '${chalk_1.default.green(runtimeEnv.network.config.endpoint)}'` + `, ChainId: '${chalk_1.default.green(runtimeEnv.network.config.chainId)}' }`); console.log("==========================================="); for (const file of scriptNames) { let relativeFilePath = file; if (file.endsWith('.ts')) { relativeFilePath = path_1.default.join('build', file.split('.ts')[0] + '.js'); } mocha.addFile(relativeFilePath); } const testFailures = await new Promise((resolve, reject) => { mocha.run(resolve); }); process.exitCode = testFailures; } async function executeTestTask({ tests }, runtimeEnv) { const logDebugTag = "wasmkit:tasks:test"; const currDir = process.cwd(); if (tests === undefined) { tests = []; for (const file of fs_extra_1.default.readdirSync(project_structure_1.TESTS_DIR)) { if (file.endsWith('.ts') || file.endsWith('.js')) { const relativeFilePath = path_1.default.join(project_structure_1.TESTS_DIR, file); tests.push(relativeFilePath); } } console.log(`Reading test files in ${chalk_1.default.cyan(project_structure_1.TESTS_DIR)} directory`); console.log(`Found ${tests.length} test files: ${chalk_1.default.green(tests)}`); } const nonExistent = filterNonExistent(tests); if (nonExistent.length !== 0) { throw new errors_1.WasmkitError(errors_list_1.ERRORS.BUILTIN_TASKS.RUN_FILES_NOT_FOUND, { scripts: nonExistent }); } await (0, scripts_1.buildTsScripts)(tests, { baseUrl: currDir, paths: { "*": [ "node_modules/*" ] }, target: ts.ScriptTarget.ES2020, outDir: path_1.default.join(currDir, "build"), experimentalDecorators: true, esModuleInterop: true, allowJs: true, module: ts.ModuleKind.CommonJS, resolveJsonModule: true, noImplicitReturns: true, noImplicitThis: true, strict: true, forceConsistentCasingInFileNames: true, sourceMap: true, declaration: true }); runtimeEnv.runtimeArgs.command = "test"; // used by Contract() class to skip artifacts runtimeEnv.runtimeArgs.useCheckpoints = false; await runTests(runtimeEnv, (0, files_1.assertDirChildren)(project_structure_1.TESTS_DIR, tests), logDebugTag); } function default_1() { (0, config_env_1.task)(task_names_1.TASK_TEST, "Runs a user-defined test script after compiling the project") .addOptionalVariadicPositionalParam("tests", "A js file to be run within WasmKit's environment") .setAction((input, env) => executeTestTask(input, env)); } exports.default = default_1;