Got TypeError: expect(...).toBeInTheDocument is not a function even after proper setup - jest-dom

I use Create React App and already declare this on src/setupTests.js:
import '#testing-library/jest-dom';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
But every time I use expect(anything).toBeInTheDocument() on test file, when running the test I get:
TypeError: expect(...).toBeInTheDocument is not a function
To make sure that the setupTests.js is actually run, I try to use enzyme shallow on test file and it works. So what is the problem with jest-dom actually and how to solve it?

Solved with:
import '#testing-library/jest-dom/extend-expect';
on src/setupTests.js

It is easier to add in your jest.config.js
module.exports = {
...,
"setupFilesAfterEnv": [
"<rootDir>/jest.setup.js"
]
}
and to create jest.setup.js with the content
import '#testing-library/jest-dom'
With that you don't have to import the jest-dom in every test file

Related

Can't write to a csv file from a class in kotlin

Im trying to write a class to csv. And I need or at least prefer a class that I could call that could both retrieve and eventually save the class objects as csv.
I have tried every imaginable way I can find to write files. Everytime it works flawlessly in the main activity .kt, but soon as I move any of it to a dedicated class I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
or I get told thats is a read only file system.
Heres a copy of a class that ive tried and gotten the error.
package com.example.jobndays
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.PrintWriter
class DryTest: AppCompatActivity() {
fun main() {
var fileInputStream: FileOutputStream? = null
fileInputStream = openFileOutput("bobbin.csv", MODE_PRIVATE)
val bob = "fella"
fileInputStream.write(bob.toByteArray())
}
}
I believe I've found an answer. Apparently in order to write files you need 'context', not entirely sure why, but just ask for it and stick infront of stuff that says they're unresolved.

hardhat 'pragma solidity' marked as unexpected identifier

I have been working on my ethereum project when face with issue new contracts stops compiling with error message:
blockchain/contracts/Utils.sol:2
pragma solidity ^0.8.9;
^^^^^^^^
SyntaxError: Unexpected identifier
I simply can not create a new contract anymore. It looks like there is some break in the environment.
Have you ever face with this issue? Do you have any thoughts what is wrong here?
Hardhat config is:
import { HardhatUserConfig } from "hardhat/config";
import "#nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
solidity: "0.8.9",
};
export default config;
The issue was in import in .ts file.
My current import for test is shown below:
import { expect } from "chai";
import { ethers } from "hardhat";
import { utils, BigNumber } from "ethers";
import { time, loadFixture } from "#nomicfoundation/hardhat-network-helpers";
import "../contracts/Utils.sol";
Remove import "../contracts/Utils.sol"; from import solves the issue.
It is redundant import, hardhat is able to evaluate type of the contract just by its name in the factory. The root cause though is unclear.
In Hardhat, this error occurs while running .sol Hardhat this error occurs while running .sol contract instead of .js deployment script

Angular 6 with lowdb - not getting it to work

I'm trying to create an Electron app with Angular 6 that uses lowdb as a local database.
It's all very new to me and it's trial and error, but I don't seem to be able to figure out how to overcome the following error:
I've installed lowdb in my application by using the commands
npm install --save lowdb (edit: forgot to mention I did this already)
and
npm install --save #types/lowdb
I've created a service to communicate with this "local database".
import { Injectable } from '#angular/core';
import lowdb from 'lowdb';
import { default as FileAsync } from 'lowdb/adapters/FileAsync';
import { CurrentUserModel } from '../models/current-user';
#Injectable({
providedIn: 'root'
})
export class LowdbService {
private db: lowdb.LowdbAsync;
constructor() {
this.initDatabase();
}
set( field: string, value: any ) {
this.db.set( field, value ).write();
}
private async initDatabase() {
const adapter = new FileAsync( 'db.json' );
this.db = await lowdb( adapter );
this.db.defaults( { user: CurrentUserModel } );
}
}
But when I include the service in the constructor of a component I get errors.
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb' in '/Users/paul/Projects/application-name/src/app/services'
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb/adapters/FileAsync' in '/Users/paul/Projects/application-name/src/app/services'
ℹ 「wdm」: Failed to compile.
ERROR in src/app/services/lowdb.service.ts(2,12): error TS1192: Module '"/Users/paul/Projects/application-name/node_modules/#types/lowdb/index"' has no default export.
src/app/services/lowdb.service.ts(3,14): error TS2305: Module '"/Users/paul/Projects/application-name/node_modules/#types/lowdb/adapters/FileAsync"' has no exported member 'default'.
As far as I can see I'm doing the same as mentioned in this Github comment and this Stackoverflow comment. I can't find any more documentation though.
Can somebody help me out?
Update
Using import * as lowdb from 'lowdb' seemed to solve my initial errors. But it resulted in a few other errors. See below.
ERROR in ./node_modules/graceful-fs/polyfills.js
Module not found: Error: Can't resolve 'constants' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/graceful-fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/legacy-streams.js
Module not found: Error: Can't resolve 'stream' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
First of all, if you installed just #types/lowdb, that is only typings for that library, you need to install the library itself via:
npm install --save lowdb
Then, looks like there is no default export so import lowdb from 'lowdb'; also won't work. Try is like this:
import { LowdbAsync } from 'lowdb';
import * as lowdb from 'lowdb';
import * as FileAsync from 'lowdb/adapters/FileAsync';
private db: LowdbAsync<any>; // TODO provide correct schema instead of any
private async initDatabase() {
const adapter = new FileAsync('db.json');
this.db = await lowdb(adapter);
this.db.defaults({ user: any });
}
Or you can try removing those typings (as they might be outdated) and do what their docs says:
https://github.com/typicode/lowdb/tree/master/examples#browser
import low from 'lowdb'
import LocalStorage from 'lowdb/adapters/LocalStorage'
(but when you take a look at #types/lowdb, there is no such default export so again, the typings might be outdated)
As for your other issue, take a look here:
https://github.com/typicode/lowdb/issues/206#issuecomment-324884867
typicode commented on 25 Aug 2017
#DickyKwok I think it's impossible. Can you answer this, #typicode?
I confirm, to save to disk using FileSync or FileAsync adapters you need to have access to Node fs API.
Looks like you should use LocalStorage adapter instead.
Another similar issue here: How can I use node "fs" in electron within angular 5
You could also try the solution proposed in one of answers there:
https://github.com/ThorstenHans/ngx-electron
constructor(private _electronService: ElectronService) {
this.fs = this._electronService.remote.require('fs');
}
Looks like your lowdb is missing in node_modules.
try
npm install lowdb

are we able to import and export functions in cypress.io?

there are few functions I want to use across the testing integrations in cypress.io is there a way to export / import the functions so I don't have to copy and paste the functions into each integration?
Thanks in advance for any advice
This might be useful for you.
https://docs.cypress.io/api/cypress-api/custom-commands.html#
You can define custom cypress commands and use them in your tests. ie cypress.login, cypress.clickHamburger, cypress.doSomethingCrazy
Yes. You can do it as you would do it in usual js code.
myFunction.js
export function funcName(param) {
return "Cypress is "+param;
}
myCypressTest.js
import { funcName } from "./myFunction.js";
funcName("great");
Yes, you can use import in your spec file.
For example, if you want to import a function add() from a Model.js, you can do something like:
import Model from '../../Model';
var model = new Model();
And call model.add() from your expect.

How to troubleshoot es6 module dependencies?

I am developing a React & Reflux app, which is bundled by webpack with babel-loader (v6), and I am experiencing es6 modules dependencies issues
For example, I have a component that use the reflux .connect() mixin :
import MyStore from '../stores/my-store';
const Component = React.createClass({
mixins: [Reflux.connect(MyStore)]
});
When I import all modules individually in each file like this, everything's fine.
I then tried to improve my code by using deconstructed import statements :
...in a component :
//import One from '../js/one';
//import Two from '../js/two';
//import Three from '../js/three';
import { One, Two, Three } from '../js'; // Instead
...and in js/index.js :
import One from './one';
import Two from './two';
import Three from './three';
export { One, Two, Three };
App source code files are more concise using the above technique, because I can import all components in one import line.
But when I use this, some dependencies end up beeing undefined when I use them
If I use the same updated example...
//import MyStore from '../stores/my-store';
import { MyStore } from '../stores'; // Instead
const Component = React.createClass({
mixins: [Reflux.connect(MyStore)]
});
...MyStore parameter ends up undefined in Reflux.connect method.
I tried to troubleshoot in the debugger, but I'm not really aware of what's going on with the __webpack_require__(xxx) statements in the generated bundle. There must be a circular dependency that babel-loader or webpack require could not figure out when there are the index.js files re-exporting individual modules.
Do you know any tool that can help me figure this out? I tried madge but it does not work with es6 modules, and I could not find anything that would tell me where anything is wrong
In order to get extended info about build, run:
webpack --profile --display-modules --display-reasons
It will give you bunch of information for optimisation/profiling.
import statement is used to import functions, objects or primitives that have been exported from an external module.
As per MDN doc, you can import the Modules not the directory.
import name from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as alias from "module-name";
import defaultMember from "module-name";
import "module-name";
Reference URL:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
http://es6-features.org/#ValueExportImport
https://github.com/lukehoban/es6features#modules
http://www.2ality.com/2014/09/es6-modules-final.html
As a workaround keep one file as base.js and include all your 3 files.