Is there any possible way to use require() instead of import in wdio latest? - require

Is there any possible way to use require() instead of import in wdio latest?
facing issues when upgraded to latest webdriverIO.

You can use below if it is a page object you are trying to import
//somePage.js
var somePage = function(){
this.eleClick = async function(){
//some code
}
}
module.export = new somePage();
//test.js
const page = require("somePage.js");
async function test(){
page.eleClick();
}

Related

make jest compile/transform/serve locally the module under test with puppeteer

I need to pass a function that is written in typescript which should run in the browser. The issue that I am having is that either I need to have the module I am testing transpiled and them encoded so I can pass it to the browser in puppeteer and it will run normally. This was the approach I was using, and it works. in short I was using es-build to bundle the module. and using readFile then encoding so I can, in the browser import it and run it there.
I am thinking if there is a better way to do this with jest-puppeteer? I can't use page.exposeFunction() because that is running on node environment. and passing the encoded function will give the browser ts code which is not what I want. To understand better look at the code bellow.
//file: module_under-test.e2e.test.ts
//importing does not help us because we might need the whole module encoded
import { testFn } from './module_under-test';
import fs, { readFileSync } from 'fs';
import util from 'util';
const readFile = util.promisify(fs.readFile);
//this will encode the module in a string, so it can be imported in the browser.
async function importer(path) {
return `data:text/javascript;utf-8,${encodeURIComponent((await readFile(path, { encoding: 'utf-8' })))}`;
}
describe('Basic authentication e2e tests', () => {
beforeAll(async () => {
await page.setViewport( {
width: 1920,
height: 1080,
deviceScaleFactor: 1
} );
//we do stuff like opening the page and logging in, etc
});
it('testToRunOnBrowser', async () => {
//module should be already transpiled but this was the old approach. I would use importer from the dist folder.
//with this the test pass but we don't want to have to transpile code everytime to run it.
//since we could already do it with only esbuild and puppeteer
expect(await page.evaluate(testToRunOnBrowser,await importer('../dist/module_under-test.mjs'))).toBe(true);
})
});
export async function testToRunOnBrowser(deps) {
const {testFn} = await import(deps)
const ctx = new browserGlobalFunctionCtx();
const data = ctx.DoGLobalBrowserThings();
ctx.load(data);
const dataLoaded = await testFn()
return dataLoaded === 'what I want to assert'
}
One way I did think but I was not able to do, is servng the whole src folder since the code from this project should all be tested on the browser. With that I can use babel standalone with "#babel/plugin-transform-modules-umd" and just import ts on the browser. any ideas or pointers how to do that with jest-puppeteer?

Testing async methods using Mocha, Chai, node.js

I have a very simple code structure like this
TestWorks.ts
const axios = require('axios');
export class TestWorks{
async getUsersList(param1:TestModel, userDetail:any){
console.log("BEGIN -- ... ");
And then this is my test class
MyTest.ts
const testworks = require("../src/interfaces/TestService/TestWorks");
it('Get Users', async () => {
var x = await testworks.getUsersList({}, {});
expect(x).to.be.an("object");
});
but I am seeing the following error, unable to figure out what the issue could be. The paths are definitely right, not an issue with the file paths of where the files are
Get Users:
TypeError: testworks.getUsersList is not a function
at C:\Users\xxxxxx\Documents\xxxxx\test\test-server.test.ts:53:28
testworks refers to the module (or whatever TypeScript exports) because you use require(). You should use import for TypeScript modules.
import { TestWorks } from '../src/interfaces/TestService/TestWorks';

module import work with react in dev but not in build

I am trying to have the same module working with both Node.js and React. This works fine until I try to create a build application with React.
Here is what I am doing
var DummyTest = function DummyTest(){
this.hello=function(){
console.log("Hello Dummy Test");
}
}
module.exports = DummyTest;
In Node.js:
const DummyTest = require("./src/utils/dummy")
var test = new DummyTest();
test.hello();
In React:
import {DummyTest} from './utils/dummy';
var test = new DummyTest();
test.hello();
Perfect, works both on Web and Node.js.
But if I try to create a build environment:
Attempted import error: 'DummyTest' is not exported from './utils/dummy'.
export default can not be used because it must works also on Node.js .
Use
import * as DummyTest from './utils/dummy
...or depending on your bundler's configuration, just require it like you would in Node.

Import Module in ES6 Class Function

I have migrated my project to ESM and thus using .mjs in all my files in nodejs.
Previously in CommonJs, I could require a file right in the middle of a ES6 class function in order to load it only when needed.
module.exports = class Core{
constructor() {
this.init = this._init.bind(this)
return this.init()
}
async _init(){
const module = require('module')
//use required file/module here
}
}
But now when using Michael Jackson Scripts a.k.a .mjs, I cannot import a file on demand:
import Koa from 'koa'
export default = class Core{
constructor() {
this.init = this._init.bind(this)
return this.init()
}
async _init(){
import module from 'module'
//use imported file/module here
}
}
My app has many files/modules that are not consumed immediately, and can more can always be added in future, thus hardcoding the imports at the begining of the file is not an option.
Is there a way to import the files dynamically on demand when needed?
With a little modification from this answer, I managed to get it working via:
import Koa from 'koa'
export default = class Core{
constructor() {
this.init = this._init.bind(this)
return this.init()
}
async _init(){
const router = await import('./Router')
//use imported file/module here
}
}
Or you can use a promise if you are into that:
import('./router')
.then(something => {
//use imported module here
});
This suits me for now until the spec if finalised and shipped

Does node-chakracore support WASM (Web Assembly)?

Can I deploy the same WASM javascript modules to node-chakracore as I can to nodejs v8?
ChakraCore has supported WebAssembly since v1.4, and node-chakracore has supported it via JavaScript since 8.x:
WASM is supported in Node-ChakraCore if you're using the WebAssembly
methods from JavaScript. Using basic.wasm from here, the following
code worked with Node-ChakraCore:
const fs = require('fs'); const buf = fs.readFileSync('basic.wasm')
async function test() {
try {
const module = await WebAssembly.compile(buf);
const inst = new WebAssembly.Instance(module, {test: {foo: function(a){console.log(`foo called: ${a}`); return 2;}}});
console.log(inst.exports.a(1));
} catch (reason) {
console.log(`Failed: ${reason}`)
} }
test();
https://github.com/sass/node-sass/pull/1777#discussion_r127280773
Alternatively, you can use node-wasm to load your wasm file and then in your node js app, do this:
import loadWasm from 'node-wasm';
async function run() {
const {rust_function} = await loadWasm('/local/path/to/wasm');
const result = rust_function();
console.log(result);
}
run();
There's a complete example here in the same repo. Good luck!

Resources