Jest test to see if *.spec.tsx files exist - jestjs

I have a TypeScript project that contains a bunch of files in different directories all located under src/.
Is it possible to write a Jest test so that it only returns success if each *.tsx file found under the parent directory (src/) has a corresponding *.spec.tsx test file?
In other words if I have the following 3 files:
src/index.tsx
src/foo.tsx
src/folder/component.tsx
The Jest test would fail until such time that the following files are created:
src/index.spec.tsx
src/foo.spec.tsx
src/folder/component.spec.tsx

I ended up using
import fs from 'fs';
and then using fs.readdir and fs.statSync to determine if a file is a file or a directory so that I can recursively look in every location from my src/ path.

Related

How to exclude files from exports in package.json in Node?

My util lib has the following exports in the package.json file
"exports":{
"./*": "./src/*.ts"
},
This works fine, however it exports all my test files. How can I exclude them?
The test files are next to the regular files, and have the extension .test.ts, e.g.:
/foo.ts
/foo.test.ts
https://nodejs.org/api/packages.html#package-entry-points

Accessing typeDefs in the dist folder | NestJS/GraphQL

Context: I had a project at which backend was written with GraphQL/lambdas. Now I want to convert whole project to NestJS/GraphQL.
I have joined.graphql file in src/GraphQL folder and there is an import (usage) of this file in another .ts file. When I try to start the project with nest start command, none of *.graphql files are copied to dist folder. Then, that import throws an error as there is no joined.graphql file in the dist folder.
I tried to add nest-cli.json file with assets field to include ./**/*.graphql files as assets. Now the issue is indeed fixed. But...
Problem:
Another error occured as
/<MY_PROJECT>/dist/src/GraphQL/joined.graphql
type Query {
^^^^^
SyntacError: Unexpected identifier
The code was trying to import typeDefs. I think there should be something a bit tricky since I'm not starting the project with nest but trying to convert to nest. Maybe missing some steps to work correctly with graphql. I'm not sure.

Is there a way to use the folder name as default resolver (instead index.js) filename when import or require?

As in the node documentation:
If there is no package.json file present in the directory, then
Node.js will attempt to load an index.js or index.node file out of
that directory. For example, if there was no package.json file in the
above example, then require('./some-library') would attempt to load:
./some-library/index.js
./some-library/index.node
So when we give the directory without the filename it looks automatically to index.js and index.node. Is there a way to look first for the name of the folder for the file? For instance:
I have a module in "Afolder/" directory, with the name Afolder.js and I use:
import module from 'Afolder';
Here what I want is that node automatically looks first for Afolder.js and then for index.js and index.node .

Zip Files & Folders With No Base Directory

AWS Lambda requires a zip file that produces a file when it's unzipped.
However, every node.js zip library produces a zip file that contains a base folder, containing the files Lambda needs, which breaks Lambda, resulting in a 'Cannot find module' error.
For example, if I have a index.js file and a node_modules directory in the dist folder, when I use gulp-zip, I get an added root folder when the zip file is unzipped...
gulp.src(['./dist/**/*'])
.pipe(zip('dist.zip'))
.pipe(gulp.dest('./'))
// When unzipped, this results in a "dist" folder containing index.js and node_modules
I've tried 6 node zip libraries and none have a simple way of excluding the base directory.
Any thoughts?
I've used 'node-archiver', which can zip a directory to a destination directory (which I just set as an empty string).
https://github.com/archiverjs/node-archiver#directorydirpath-destpath-data
var archiver = require('archiver');
archive = archive.directory('./directoryToZip/', '' ); //option 2 is the dest
archive.pipe( outZip);
archive.finalize();

Require.js r.js: Compile directory to single file

Is there any way to configure RequireJS to compile an entire directory to a single file? I don't mean the normal use case of the 'out' setting. I'll try to explain by example. If I have the following project structure:
- app
- main.js
- menu.js
- module
- file-a.js
- file-b.js
Then let's say I want to compile the 'app' directory to a single file. I don't care about it's dependencies - even if it requires 'module' or either of its files, they won't be included. Even if main.js doesn't require menu.js, it'll be included anyway. The resultant output file would define 'app/main' and 'app/menu' modules.
Likewise, if I wanted to compile the 'module' directory, the file would define 'module/file-a' and 'module/file-b' regardless of what other dependencies were defined.
I hope this is clear enough.
You can use the dir parameter in build file of require instead of just name parameter.
You can read more about building whole directory on requirejs documentation - Optimize Whole Project
If you write build file something like app-build.js-
({
appDir: ".",
baseUrl: "app",
dir: "../app-build",
})
and if you run r.js -o app.build.js then it will create
app-build
main.js
menu.js
Here menu.js will not be include in main.js unless it is required somewhere in main.js source.

Resources