Issues with importing a library in nim test file - nim-lang

I'm having some issues including a library in a nim test file.
My code in my test file looks like this.
import unittest
import testToolpkg/Login
import dotenv
load()
test "valid login cookies":
var cookies = loginCookies(os.get("EMAIL"), os.get("PASSWORD"), os.get("URL"))
check cookies.len != 0
However when I run nimble test I get this error Error: cannot open file: dotenv. This seems to be an issue with any external library I try to use in a test.
The dotenv library works fine outside of the tests folder. I'm not sure if I'm missing some kind of option to get this to work? I am running this on windows as well.

After adding the lib to my .nimble file
requires "dotenv >= 2.0.0"
The issue was resolved!

Related

Why I'm getting Error [ERR_MODULE_NOT_FOUND]: Cannot find module when running the server

I'm doing a startup server. Basically It has initial setup with express and nodemon dependencies. Unfortunately, I'm getting ERR_MODULE_NOT_FOUND when running yarn dev or npm run dev
Here is the code and file structure I have.
The issue is a missing file extension. The import needs to look like this:
import { sampleExport } from "../config/env.js";
// ^^^
import doesn't use the same algorithm that require used. Instead, it works according to the ECMAScript Modules specification, which requires a definite extension.
This is also explained in the node.js docs for ECMAScript modules:
A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. './startup/index.js') must also be fully specified.
This behavior matches how import behaves in browser environments, assuming a typically configured server.
You need to add .js extension
index.js:
import { SampleExport } from "../path/file.js"
In my opinion, it is better to use .env files together with some npm package or even a json file.

Jest failing to run tests when importing globals

I'm trying to follow the advice in https://jestjs.io/docs/en/api about importing globals, namely this:
However, if you prefer explicit imports, you can do import {describe, expect, it} from '#jest/globals'.
It doesn't work, though. I added the import at the top of my two test files and when I run jest, both test suites fail with the message "Do not import #jest/globals outside of the Jest test environment."
I also have jest configured in eslint env, in case it makes a difference.
Can someone point me in the right direction, please?
I just found out what the problem was: I was importing the #jest/globals package in a helper file which isn't a test file. Removing the import from there and leaving it only in the two test files allows Jest to run without a problem.

How to use the "debug" module with typescript

I have an NTVS (Node Tools for Visual Studio) project with Typescript.
The following statement doesn't compile:
import debug = require('debug')('MyApp');
The syntax error being
(TS) ';' expected
between the the two parenthesis ')('
Is it possible to use "debug" with TypeScript?
From the README, debug module is exporting a function that decorates console.error with your module name (MyApp). I'm guessing there are other ways, but I use:
import Debug from "debug";
const debug = Debug("MyApp");
// then to use
debug("Something happened");
And to print everything to the console, run your app with...
$ DEBUG=* node MyApp.js
The answers here did not work for me with more recent versions of Typescript. Here's how I got it working with proper import syntax in Typescript ^3.5.3:
Install Debug package and Typescript types for Debug (types only needed for dev)
npm install --save debug
npm install --save-dev #types/debug
Then in .ts files:
import Debug from "debug";
const debug = Debug("AppName");
Hope this helps someone else!
Remember that TypeScript is a super-set of javascript, so you can still also do this one-liner:
const debug = require('debug')('my-app:my-module');
Typescript seems to conclude that the debug constant here is of type 'any', and you lose all type safety, but with a package as simple as debug is, I think you will be OK...
Personally, I think 2 lines to instantiate debugging in every module is 1 line too many, so I continue to use this one-liner in my .ts files.
P.S. I like to use module tags so I can enable logging in just certain modules with DEBUG=my-app:my-module,my-app:some-other-module ts-node my-app or all my modules with DEBUG=my-app:* ...
The solution to debug not showing any logs in typescript is due to the reason that debug relies on the environment variables to decide how to show the logs
Solution
Make sure you have installed dotenv and its type definition file
npm install dotenv && npm install -D #types/dotenv
Then create a .env file at the root folder of your project add and this environment variable:
DEBUG = *
finally at the index file of your application. Configure dotenv to load the environment variables before any other task is run.
Its very important dotenv configuration is done at the top of the index file, before any other lines of code.
Add this two lines of code
import dotenv from "dotenv";
dotenv.config();
This should load the DEBUG environment variable required by debug to show output on the standard input.
Remember if you want to view logs defined in your files only and not other logs from other modules. Its better you define your application name as the namespace. That way you can filter the logs by the environment variable described above i.e.
const debug = debug("applicationName:other-more-information");
Then to view your debug logs alone just change the DEBUG varibale from * to applicationName:*
DEBUG = applicationName:*
For unix based os users you can try exporting this variable to enviroment variables directly (Though i have not tested this solution) export DEBUG=* - This method will only work for all process started on this shell

How to use ES6 import with 'request' npm module

In ES6-ifying some TypeScript code (the project I'm working runs in both the browser and a Node server, I'd like to tree-shake the browser bundle), I'm trying to eliminate uses of require and only use import. But when I do this...
import * as request from 'request';
and subsequently call request(), I get runtime errors in Node (after using babel to make the code ES5, and thus Node, compatible):
TypeError: request is not a function
On the other hand, if I do this:
import request from 'request';
then the TypeScript compiler complains with
error TS1192: Module '"<mypath>/node_modules/#types/request/index"' has no default export.
If I manually change the compiled JS code to use import request from 'request';, it actually works fine... how can I force the TS compiler to accept this code and just pass it through?
Can you try Add allowSyntheticDefaultImports: true to your 
tsconfig.json
 seems like still an open issue in Typescript.
Basically you need remove comment in tsconfig.json at line 46, because default ts config file have this option but it's commented by default

TypeScript won't resolve external module (node.js)

I would like to use moment.js in my node application, so I installed moment.js using node's package manager npm:
npm install moment#2.4.0
Just to be on the safe side, I checked moment is not installed globally and the installed version is really version 2.4.0 (version 2.4.0 in order to use the correct d.ts file ...)
require("moment").version
Alright, seems to be good. I'm also using the latest version of TypeScript (0.9.5).
So, now I added the following file to my projects root directory https://github.com/borisyankov/DefinitelyTyped/blob/master/moment/moment.d.ts and refernced the file:
/// <reference path="moment.d.ts" />
Now, it should work to import moment using TypeScripts import keyword:
import m = require("moment");
Compiling with the following command
tsc app.ts --module commonjs
produces the following errors
/home/unknown/temp/test/app.ts(3,1): error TS2071: Unable to resolve
external module '"moment"'. /home/unknown/temp/test/app.ts(3,1): error
TS2072: Module cannot be aliased to a non-module type.
Why does this error occur? How do I fix it?
The important line in the d.ts file is this one...
declare var moment: MomentStatic;
It just declares a variable for moment.
You can add the following line to resolve your problem:
export = moment;
This should make it loadable using the import statement you have.
If you do this - you won't need yhe reference comment.

Resources