Testing plugins with mocha

Has anybody tested the plugin using mocha or jest? I tried to run it, but I get errors all the time.

My code is
index.ts

export function decreaseHeaderLevel(selection: string): string {
	return selection.replace(/^(#+)\s./, '#');
}

index.spec.ts

import { decreaseHeaderLevel } from 'src/index';

var assert = require('assert');
describe('Decrease level header', function () {
    describe('function', function () {
        it('should replace ## with #', function () {
            assert.equal(decreaseHeaderLevel('### rient'), '## rient');
        });
    });
});

package.json

"test": "ts-mocha -p tsconfig.json src/**/*.spec.ts"

tsconfig.json

{
	"typeRoots": [
		"types"
	],
	"compilerOptions": {
		"outDir": "./dist/",
		"module": "commonjs",
		"target": "es2015",
		"jsx": "react",
		"allowJs": true,
		"baseUrl": ".",
		"paths": {
			"@/*": [
				"src/*"
			]
		}
	},
	"include": [
		"src/**/*.ts",
		"tests/**/*.ts"
	],
	"exclude": [
		"node_modules",
		".vscode"
	]
}

And I get errors:

  1. Running mocha
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/kamil/Documents/IT/projects/joplin-formatter-plugin/src/index.spec.ts
  1. Running ts-mocha
> joplin-plugin-my-formatter@1.0.0 test
> ts-mocha -p tsconfig.json src/**/*.spec.ts


Error: Cannot find module 'src/index'
Require stack:
...
    at Object.<anonymous> (/home/kamil/Documents/IT/projects/joplin-formatter-plugin/src/index.spec.ts:1:1)

Yes with jest in my projects joplin-plugin-backup and joplin-plugin-note-overview

1 Like