IntersectionObserver is not defined - ts-mockito

I'm using ts-mockito with jsdom-global to add unit tests to a project which uses IntersectionObserver but unfortunately I'm stumbling on the following error:
ReferenceError: IntersectionObserver is not defined
Does ts-mockito provide any way to mock or override IntersectionObserver?

Related

Mocha test reporting Object.hasOwn is not a function

I am using mocha and sinon to test and I am using node 16.19.0
I am using Object.hasOwn in my code and the test is currently failing with:
"Object.hasOwn is not a function"
I'm not really sure why is doesn't exist in the first place.
I have tried to stub it
Sinon.stub(Object, 'hasOwn').returns({});
but I get the error:
Cannot stub non-existent property hasOwn
If I monkey patch it the issue goes away.
Object.hasOwn = Sinon.stub().returns({});
What is the correct approach here?

How to implement AsyncTransformer in Jest?

I am trying to use jest with the esbuild based project. I tried to use esbuild-jest-transform and esbuild-jest transformers, but in both cases, I got error Cannot use plugins in synchronous API calls from esbuild. This makes sense and I tried to implement AsyncTransformer instead.
But after switching from SyncTransformer to AsyncTransformer, I got the following error:
● Invalid synchronous transformer module:
".../node_modules/esbuild-jest-transform/index.js" specified in the "transform" object of Jest configuration
must export a `process` function.
which implies that it should be implemented in the old way. Here are more details about it.
Could you please tell, me how to make jest to use AsyncTransformer instead of SyncTransformer?
I tried to use import() as is described in the doc, but got the same result.

How can I declare global Error type in typescript NodeJs Architect project?

Context
Hi, I have a NodeJs+Ts+Architect setup for building and deploying lambda functions. Architect uses typescript plugin to compile typescript. I am trying to use Error class to throw errors.
However, Typescript is picking up Error type from
/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/lib.es5.d.ts.
In the image below, please note the constructor signature only accepts message field. And the error interface does not have an options object either. Please
look at Browser Error Class or NodeJs Error Class to see the signatures.
Node Error has the following constructor signature and Error interface.
Problem
Getting TS Error for trying to use constructor signature of Node Error as Typescript is reading Error type from lib.es5.d.ts which accepts only 1 argument
Possible Solutions which I know
Declare global Error type ( Need help here. Since Architect is compiling TS using its plugin, I am not able to declare and override Error interface )
Use your own Error class
I hope the question made sense. Would appreciate if there is a nicer way to solve this, but I am not getting ample discussions on Architect+Ts+NodeJs.
The cause option was introduced in ES2022. You would need to use at least that version for your lib types.
Change the lib version in your tsconfig.json to:
"compilerOptions": { "lib": ["es2022"] }

PowerMockito Argument Matching failure

I am trying to use PowerMockito to mock a private class like this:
PowerMockito.when(spyClass, "privateMethod", anyString()).thenReturn(returnList);
I have these two annotations above the test method:
#Test
#PrepareForTest(CartItemRepository.class)
And these above the class:
#ExtendWith(MockitoExtension.class)
#RunWith(PowerMockRunner.class)
There are no previous method calls (stubbed or otherwise) in the test method that could be interfering.
But, I am getting an error related to incorrect use of argument matchers.
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
I am using the when method of PowerMockito (and not regular Mockito) but the 2nd error paragraph makes it seem like mocking private methods is still not possible.
I'm not sure what I'm doing wrong here.

Elasticsearch 5.2 unit tests

I am writing unit tests for ES5.2, and am having some issues. I need to create a client which connects to a local node for running tests, so I use
esClient = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
This causes problems
java.lang.IllegalStateException: running tests but failed to invoke RandomizedContext#getRandom
So then I used an annotation on my unit test classes as
#RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
but this gave me an error
SEVERE: 2 threads leaked from TEST scope at {method_name}):
1) Thread[id=36, name={project_name}writer-thread-0, state=WAITING, group={project_name}-writer]
And the unit tests threw errors as
org.mockito.exceptions.misusing.WrongTypeOfReturnValue
I tried to use the annotation as below on the unit test class
#ThreadLeakScope(ThreadLeakScope.Scope.NONE)
That got rid of some error messages, but there were still concurrency issues in the code.
Anyone knows how to handle this? Is there some explicit thread safety methods I should use?
Excluding com.carrotsearch.randomizedtesting:randomizedtesting-runner from dependencies works.
Example (build.gradle):
dependencies {
testCompile(project(path:':other-project')) {
exclude group:"com.carrotsearch.randomizedtesting", module:"randomizedtesting-runner"
}
}
Why?
If you have randomizedtesting-runner in your classpath, ES will load it:
https://github.com/elastic/elasticsearch/blob/7.10/server/src/main/java/org/elasticsearch/common/Randomness.java#L55
And use it:
https://github.com/elastic/elasticsearch/blob/7.10/server/src/main/java/org/elasticsearch/common/Randomness.java#L102

Resources