What is differential between useFactory and useValues who using for mock my module in createTestingModule on NestJS
I read Testing | NestJS and I read the article but found no explanation about the difference or when to use either mode.
Really, nothing more than useFactory accepts a function and could be used to inject values based on the current module while useValue is a direct value. There's really not much more to it than that. They're just custom providers
Related
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.
What is the difference between toBeInTheDocument() and toBeDefined() when testing with Jest and React Testing Library? When should they be used?
The #testing-library/jest-dom library provides a set of custom jest matchers that you can use to extend jest. These will make your tests more declarative, clear to read and to maintain.
It solve the below problem:
You want to use jest to write tests that assert various things about the state of a DOM. As part of that goal, you want to avoid all the repetitive patterns that arise in doing so. Checking for an element's attributes, its text content, its css classes, you name it.
From the source code of toBeInTheDocument(), it uses several DOM APIs such as Node.getRootNode() and Node.cloneNode(). You don't need to check if the element is present in the document on your own. This is very convenient and readable.
toBeDefined has nothing to do with DOM API, it's a basic matcher uses const pass = received !== void 0; to check if the value is defined or not.
This question already has an answer here:
While creating a dynamic module in Nest.js should i use registerAsync or forRootAsync?
(1 answer)
Closed 3 years ago.
I am new to NestJS and I would like to create a Dynamic Module for the injection of a metrics reporter provider. The documentation for NestJS says the following about Dynamic Modules:
...by convention we should call it either forRoot() or register() [when creating a Dynamic Module]
Unfortunately, the documentation gives no clear guidance as to when you should implement register or forRoot or what the expectations in the implementation of the two functions should be. This leads me to believe that I could call the function cheeseburger and as long as it returns a DynamicModule.
What I like to do is use forRoot/forRootAsync for initial module configurations. From there if I need to be able to pass in new options for some services i like to use forFeature. This helps me keep in mind that the DyanmicModule is configured in the root of my application (usually AppModule) and can be used elsewhere in the application, either via forFeature() or by importing the expected module. Otherwise, you are absolutely right, you could call it cheeseburger and it would be just fine. No differences other than what others using the package might be expecting
I'm reading the mocking capability of minitest.
require "minitest/autorun"
mock = MiniTest::Mock.new
mock.expect(:use_any_string, true, [String])
mock.use_any_string("foo")
## mock.use_any_string(1)
## MockExpectationError: mocked method :use_any_string called with unexpected arguments [1]
## I do not understand the purpose for this
mock.verify
So I do not understand the purpose of using mock.verify since the trying to pass any another type(to use_any_string) other than String result in mock expectation error.
So why should one use assert mock.verify then?
You are right, you cannot set anything else to the configured mock, but if your mock is not called at all, then you can find out with mock.verify. So if your method under test should call the mocked method, you should verify that it was called at all.
You are correct, mock.expect(:use_any_string, true, [String]) will cause the test to fail if the expected call is not made on the mock.
However using mock.verify depends on how you compose your test. In your example you are explicitly calling the method you are testing with mock.use_any_string("foo") so there is no need to also call mock.verify.
You could get into a scenario where you are not making this call explicitly, or are expecting the mock to have had calls made at a certain time. These scenarios would take advantage of mock.verify calls.
(Another note, minitest has recently introduced assert_mock which can be used instead of mock.verify)
If I create my httpBuilder as shown below (assume that a proxyUsername IS set, so setCredentials is called), then calls to httpAddress-es that are passed in properly are routed through the proxy. However, the Application has some http calls that are within the local network. Can http.nonProxyHosts be used to work around this and bypass the Proxy? If so, how? Use System.setProperty? Or something on HttpBuilder?
HTTPBuilder httpBuilder = new HTTPBuilder(httpAddress)
httpBuilder.setProxy(webProxyHost, webProxyPort, webProxyProtocol)
if (proxyUsername) {
httpBuilder.client.getCredentialsProvider().setCredentials(
new AuthScope(webProxyHost, webProxyPort),
new UsernamePasswordCredentials(proxyUsername, proxyPassword))
}
}
In the code above, all of the various named elements (webProxyHost, etc) are declared as String and set accordingly.
In answer to the question in the above comment, our primary 'nonProxyHost' need was for 'localhost' which is there by default. Thus this ceased to be an issue. Did not ever really find out how to accomplish this as it is somewhat version-specific on HttpClient.
You can set the System property:
System.setProperty('http.nonProxyHosts', myNonProxyHosts)
However, if you call 'setProxy' on HttpBuilder, even if you call 'useSystemProperties' it will not. This is in their documentation, just not obvious!
Finally, you might be able to call:
httpBuilder.client.params.setParameter('http.nonProxyHosts', myNonProxyHosts)
But I do not know for sure if that is the property name and documentation of those properties is hard to find. Worse - those 'params' are deprecated - you are supposed to use the better 'config' classes, though once again finding comprehensive documentation on all the parameters for that is not the easiest! Wish I could have been of more help!