小编Fab*_*eli的帖子

NodeJS使用mock-fs测试writeFile

例如,我有以下文件:

规格/配置/文件mock.js

var mock = require('mock-fs');
mock({
  'files': {
    'expense.csv': 'a;b;c;d\n1;2;3;4\n5;6;7;8'
  }
});
Run Code Online (Sandbox Code Playgroud)

应用/阅读/ reader.js

var fs = require('fs');
var reader = {
    read: function(path) {
        return fs.readFileSync(path, 'utf8');
    },
    write: function(path, object) {
        fs.writeFileSync(path, object);
    }
};
module.exports = reader;
Run Code Online (Sandbox Code Playgroud)

应用/阅读/ reader.spec.js

describe('reader.js test', function(){
    var reader = require('./reader.js');
    var mock = require('mock-fs');
    it('should return a simple string', function(){
        expect(reader.read('files/expense.csv')).toEqual('a;b;c;d\n1;2;3;4\n5;6;7;8');
    });
    it('should write a json object', function(){
        // WHAT TO DO?!
    });
});
Run Code Online (Sandbox Code Playgroud)

reader.read函数与mock-fs工作正常.

但我正在尝试测试该 …

javascript unit-testing mocking node.js

9
推荐指数
1
解决办法
1852
查看次数

如何获得不保存到的package.json包的列表?

我已经安装了几个包在我的本地机器,但我无法知道我已经安装了哪些。

有一些框架,告诉我哪些软件包和版本安装在我的项目(未保存的,保存和开发保存)?

我试图比较node_modules文件夹,但结果不是很精确。

npm

4
推荐指数
1
解决办法
399
查看次数

NestJS - 如何在 JWT 中使用 RoleGuard?

目前,我已经设法将 AuthGuard 与 JWT 身份验证结合使用,但我无法userRoles.

我试图按照猫的例子,但我从来没有user定义过对象,你可以在第 14 行看到。

这是我的代码:

// auth.controller.ts
@Controller('auth')
@UseGuards(RolesGuard)
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Get('token')
  async createToken(): Promise<any> {
    return await this.authService.createToken();
  }

  @Post()
  @UseGuards(AuthGuard())
  @Roles('admin')
  findAll() {
    // this route is restricted by AuthGuard
    // JWT strategy
    return 'Super important info';
  }
}


// auth.module.ts
@Module({
  imports: [
    SharedModule,
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      imports: [SharedModule],
      useFactory: async (configService: ConfigService) => ({
        secretOrPrivateKey: …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript nestjs

1
推荐指数
1
解决办法
3005
查看次数

标签 统计

javascript ×2

node.js ×2

mocking ×1

nestjs ×1

npm ×1

typescript ×1

unit-testing ×1