Skip to content

Instantly share code, notes, and snippets.

@jeremiahlukus
Last active February 4, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremiahlukus/5db47538e53eb5a103e77e3d55f2d4f2 to your computer and use it in GitHub Desktop.
Save jeremiahlukus/5db47538e53eb5a103e77e3d55f2d4f2 to your computer and use it in GitHub Desktop.
jest config
module.exports = {
moduleFileExtensions: ['ts', 'js'],
resetMocks: true,
restoreMocks: true,
testEnvironment: 'node',
testRegex: '\\.test\\.(ts|tsx)$',
transform: {
'^.+\\.ts?$': 'babel-jest'
},
transformIgnorePatterns: [
'node_modules/(?!(typeorm)/)'
]
};
.env
TYPEORM_CONNECTION = "postgres"
TYPEORM_HOST = "localhost"
TYPEORM_USERNAME = "jparrack"
TYPEORM_PASSWORD = ""
TYPEORM_DATABASE = "test"
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = "src/entity/**/*.ts"
TYPEORM_AUTO_SCHEMA_SYNC = true
TYPEORM_SUBSCRIBERS = src/subscriber/*.ts
TYPEORM_MIGRATIONS = src/migration/*.ts
TYPEORM_ENTITIES_DIR = src/entity
TYPEORM_MIGRATIONS_DIR = src/migration
TYPEORM_SUBSCRIBERS_DIR = src/subscriber
babel.config.js
module.exports = {
sourceMaps: 'both',
presets: [
'@babel/preset-env',
"@babel/preset-typescript",
],
env: {
test: {
presets: [
'@babel/preset-env',
"@babel/preset-typescript",
],
plugins: [
'transform-es2015-modules-commonjs',
'babel-plugin-dynamic-import-node',
'source-map-support',
'transform-class-properties',
['@babel/plugin-proposal-decorators', {legacy: true}],
//['@babel/transform-runtime', { regenerator: true }],
],
},
},
};
tsconfig.json
{
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "es5", "es6"],
"target": "es5",
"moduleResolution": "node",
"sourceMap": true,
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"noImplicitAny": true,
"noUnusedLocals": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules"],
"types": ["typePatches", "node"]
}
index.test.ts
import {User} from "./entity/User";
import {done} from "./index";
describe('addBetaFeature', () => {
describe('valid request', () => {
it('should return statusCode 200', async () => {
const user = await User.create({
firstName: 'testFName',
lastName: 'Lastname',
age: 23
}).save();
const response = await done
expect(response).toBe("hey");
})
})
})
typeorm-congif.ts
import {User} from "./entity/User";
require('dotenv').config()
import 'reflect-metadata';
export const clientConfig = {
database: process.env.TYPEORM_DATABASE,
type: process.env.TYPEORM_CONNECTION,
host: process.env.TYPEORM_HOST,
port: process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
entities: [
User
],
synchronize: true,
}
index.ts
import "reflect-metadata";
import '@babel/polyfill';
import {createConnection} from "typeorm";
import {User} from "./entity/User";
import {clientConfig} from "./typeorm-config";
export const done = createConnection(clientConfig).then(async connection => {
console.log("Inserting a new user into the database...");
const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await connection.manager.save(user);
console.log("Saved a new user with id: " + user.id);
console.log("Loading users from the database...");
const users = await connection.manager.find(User);
console.log("Loaded users: ", users);
console.log("Here you can setup and run express/koa/any other framework.");
}).catch(error => console.log(error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment